UITableView(一)

  • UITableView简单使用步骤

    1. 设置数据源dataSource
    // 设置数据源
    self.tableView.dataSource = self;
    
    1. 实现<UITableViewDataSource>协议
    2. 实现<UITableViewDataSource>协议中的- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法

/**
 *  标记多少行数据
 *
 *  @param tableView tableView
 *  @param section   组号
 *
 *  @return 返回行数
 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;// 两条
}


/**
 *  每行如何显示
 *
 *  @param tableView tableView
 *  @param indexPath 
 *
 *  @return 每行的内容
 */
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
        // initWithStyle:UITableViewCellStyleSubtitle 显示子标题
//    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    cell.textLabel.text = @"我是第一组";
    cell.imageView.image = [UIImage imageNamed:@"m_10_100"];//添加图片
    
    if(indexPath.section == 1)
    {
        cell.textLabel.text = @"我是第二组";
        cell.imageView.image = [UIImage imageNamed:@"m_13_100"];
    }
    
    if(indexPath.section == 2)
    {
        cell.textLabel.text = @"我是第三组";
        cell.imageView.image = [UIImage imageNamed:@"m_14_100"];
    }
    
    return  cell;
}

  • 分组的UITableView

    • 把UITableView的style属性改为Grouped
    • 实现-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView方法
     /**
    *  分组
    *
    *  @param tableView tableView
    *
    *  @return 返回几组
    */
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
       return 3;// 两组
    }
    
  • 给UITableView添加头尾描述部分

    • 实现-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section方法
      -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
      {
          return @"我是头";
      }
    
      -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
      {
          return  @"我是尾";
      }
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,101评论 3 38
  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 10,751评论 3 3
  • UITableView 表格视图一 UITableView1.1是什么?以列表的方式展示数据的一种控件,且继承自...
    037e3257fa3b阅读 262评论 0 1
  • 一、UITableView的概念: UITableView 是iOS中最重要的控件,几乎所有的页面都可以用UITa...
    曉明儿阅读 534评论 0 2
  • style //普通UITableViewStylePlain,//分组UITableViewStyleGroup...
    seventhboy阅读 373评论 0 0