iOS开发(UI基础阶段, 小白请看) UITableView的基本用法1

UI基础知识汇总 (part 2)


代码部分

UITableView数据源方法

  • 设置控制器为UITableView的数据源(别忘了遵守<UITableViewDataSource>协议)

    self.tableView.dataSource = self;
    
  • 返回一共有多少组数据的方法

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return self.groups.count;
    }
    
  • 返回每组有多少行数据的方法

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        //获取模型数据
        WMUGroup *group = self.groups[section];
        return group.cars.count;
    }
    
  • 返回每行要显示的cell的方法

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        //1.获取模型数据
        WMUGroup *group = self.groups[indexPath.section];
        WMUCar *car = group.cars[indexPath.row];
        //2.创建cell
        static NSString *ID = @"car_cell";
        //2.1利用标识符ID重用cell
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        //2.2如果缓存池中没有能重用的cell
        if (cell == nil) {
          //2.3那么才创建新的cell并绑定标识符
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        }
        //3.设置cell属性
        cell.textLabel.text = car.name;
        cell.imageView.image = [UIImage imageNamed:car.icon];
        //3.1设置cell最右侧的辅助指示图标
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        //3.2在cell的最右侧添加一个开关
        cell.accessoryView = [[UISwitch alloc] init];
        //4.返回cell
        return cell;
    }
    
  • 设置头标题的方法

    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        WMUGroup *group = self.groups[section];
        return group.title;
    }
    
  • 设置尾部描述文字的方法

    -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
        //取出模型
        WMUCar *car = self.cars[section];
        //返回模型中的品牌系描述属性
        return car.desc;
    }
    
  • 显示右边索引栏方法

    -(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
        //kvc取值法
        return [self.groups valueForKeyPath:@"title"];
    }
    
  • 设置cell之间分隔线的颜色

    self.tableView.separatorColor = [UIColor redColor];
    
  • 设置分割线样式

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
  • 添加一个headView

    self.tableView.tableHeaderView = [[UISwitch alloc] init];
    
  • 添加一个footerView

    self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeContactAdd];
    

UITableView的代理方法

  • 让控制器成为UITableView的代理(别忘了遵守<UITableViewDelegate>协议)

    self.tableView.delegate = self;
    
  • 设置行高的代理方法

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.row % 2 == 0) {
            return 60;
        } else {
            return 100;
        }
    }
    
  • 选中某行的的代理方法

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    }
    

欢迎大家留言评论!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容