UITableView基础知识

1.继承自UIScrollView

   两种样式都能够分组,只不过是分组显示的效果不同而已

     1.UITableViewStylePlain (列表样式)

       UITableViewStylePlain分组效果,头部标题有悬停效果

     2.UITableViewStyleGrouped (有明显分组效果的样式)

       UITableViewStyleGrouped分组,组与组之间有明显的分隔效果

  创建UITableView

    UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

    [self.view addSubview:tableView];

    

  想要正确的显示出来数据,要保证两点

    1.设置了tableview的协议实现对象

    2.实现datasource协议中必须要实现的方法

    必须实现以下两个协议方法,不实现就报错

        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section     

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

 

    tableView.dataSource = self;

--------------------------------

#pragma mark UITableViewDataSource

  //1.你要让tableview显示多少组数据?

 //可以不实现,默认值是1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

 
 

 //2.每一组有多少行数据?

 //如果有多组,怎么回答这个问题?

//必须要实现的协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

 

 

//3.每一组的每一行,显示什么样的具体数据?

//UITableViewCell 继承自UIView的特殊视图

//一个UITableViewCell就代表,UITableView中的一行数据

 //cellForRowAtIndexPath 会被调用多少次呢?

//必须要实现的方法

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

 

//头部

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

 

//尾部

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

 

#pragma mark UITableViewDelegate
//点击哪一行就跳转到哪一行所对应的页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     Item *it = [_dataArray objectAtIndex:indexPath.row];

    DetailViewController *detail = [[DetailViewController alloc] init];
    detail.title = it.name;
    [self.navigationController pushViewController:detail animated:YES];
}

 


整体设置,所有行的高度

  tableView.rowHeight = 150;

设置背景颜色

    tableView.backgroundColor = [UIColor redColor];

    UIImageView * imageView = [[UIImageView alloc] init];

    imageView.image = [UIImage imageNamed:@"img_background.jpg"];

    tableView.backgroundView = imageView;

设置分割线为no

    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    

    //自定义给  tableHeaderView添加一个UIView

    UIView * headerView = [[UIView alloc] init];

    headerView.backgroundColor = [UIColor greenColor];

    headerView.frame = CGRectMake(0, 0, 375, 200);

    tableView.tableHeaderView = headerView;

 

    //自定义给  tableFooterView添加一个UIButton

    tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeInfoLight];

    


 

  设置cell的背景颜色

    cell.backgroundColor = [UIColor grayColor];

    UIImageView * backgroundImageView = [[UIImageView  alloc] init];

    backgroundImageView.image = [UIImage imageNamed:@"selected_background"];

    cell.backgroundView = backgroundImageView;

    

  设置选中状态下的背景图片

   cell.selectedBackgroundView = backgroundImageView;

    

   用户提示

      cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

      cell.accessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

 


隐藏状态栏方法,系统自动调用

- (BOOL)prefersStatusBarHidden

{

    return YES;

}

 

posted on 2016-03-18 13:11  风过无痕CL  阅读(145)  评论(0编辑  收藏  举报

导航