UITableView创建步骤与常用数据源方法

创建步骤

  • 创建tableView对象
      UITableView *tableView=[[UITableView alloc]init];
      tableView.frame=self.view.bounds;
    
  • 实现协议UITableViewDataSource
  • 设置数据源
      tableView.dataSource=self;
    
  • 实现协议的一些方法
      //返回每一组的条数
      -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
      return 50;
    }
    //返回cell
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
      cell.textLabel.text=[NSString stringWithFormat:@"test%zd",indexPath.row];
      return cell;
    }
    
  • 此时还可以设置代理UITableViewDelegate(可选)

常用数据源方法

  • 设置有多少分组
      -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    
  • 设置每组有多少个cell
      -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    
  • 设置cell数据
      -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    
  • 设置组头标题
      - (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
    
  • 设置组尾部标题
      - (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
posted @ 2016-04-04 19:11  爱上咖啡的唐  阅读(209)  评论(0编辑  收藏  举报