前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS-UI控件之UITableView(一)

iOS-UI控件之UITableView(一)

作者头像
用户1941540
发布2018-05-11 13:51:27
1.7K0
发布2018-05-11 13:51:27
举报
文章被收录于专栏:ShaoYLShaoYL

UITableView

介绍

  • UITableView 是用来用列表的形式显示数据的UI控件
    • 举例
    • QQ好友列表
    • 通讯录
    • iPhone设置列表

tableView 常见属性

代码语言:javascript
复制
    // 设置每一行cell的高度
    self.tableView.rowHeight = 100;

    // 设置每一组头部的高度
    self.tableView.sectionHeaderHeight = 50;

    // 设置每一组尾部的高度
    //    self.tableView.sectionFooterHeight = 50;

    // 设置分割线颜色
    self.tableView.separatorColor = [UIColor redColor];
    // 设置分割线样式
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 设置表头控件
    self.tableView.tableHeaderView = [[UISwitch alloc] init];
    // 设置表尾控件
    self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeContactAdd];

UITableView的两种样式只读属性

  • 只读属性,在代码中不能修改 UITableViewStylePlain
  • 一组显示Section = 1;
UITableViewStyleGrouped
  • 分组显示Section >= 1;

展示数据

遵守协议
  • UITableViewDataSource
设置数据源
  • 连线
  • 代码
实现数据源方法
  • 先调多少组
代码语言:javascript
复制
//调用数据源的下面方法得知一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
  • 多少行
代码语言:javascript
复制
//调用数据源的下面方法得知每一组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
代码语言:javascript
复制
- 每行数据
代码语言:javascript
复制
//调用数据源的下面方法得知每一行显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

UITableView右边的索引条

  • 属性
代码语言:javascript
复制
    //设置tableView右边索引文字的颜色
    self.tableView.sectionIndexColor = [UIColor redColor];
    //设置右边索引文字背景的颜色
    self.tableView.sectionIndexBackgroundColor = [UIColor grayColor];

    //数据源方法索引信息
    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    //返回的是一个数组,数组中的元素是显示信息,只是提示,结果还是按索引位置分组
    return [NSArray arrayWithObjects:@"a",@"b",@"c",@"d", nil];
}

tableView常用方法

代码语言:javascript
复制
//设置分组的头部数据
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"头部";
}
//设置分组的尾部数据
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return @"头部";
}
**
 *  当选中一行的时候调用(点击)
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    XMGWine *wine = self.wineArray[indexPath.row];
//    NSLog(@"点击了:%@", wine.name);
    NSLog(@"选中了:%zd", indexPath.row);
}

/**
 *  当取消选中一行的时候调用
 */
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"取消选中了:%zd", indexPath.row);
}
/**
 *  返回每个cell的高度
 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row % 2 == 0) {
        return 50;
    } else {
        return 100;
    }
}

UITableViewCell

  • cell 的样式属性
    • UITableViewCellStyleDefault
    • UITableViewCellStyleValue1
    • UITableViewCellStyleValue2
    • UITableViewCellStyleSubtitle
  • cell 右边指示样式的属性accessoryView
    • 优先级高于accessoryType
  • cell 右边指示样式的属性 accessoryType
    • UITableViewCellAccessoryNone
    • UITableViewCellAccessoryDisclosureIndicator
    • UITableViewCellAccessoryDetailDisclosureButton
    • UITableViewCellAccessoryCheckmark
    • UITableViewCellAccessoryDetailButton
  • cell 被点击的颜色变化iOS 7 之前的
    • UITableViewCellSelectionStyleNone
    • UITableViewCellSelectionStyleBlue
    • UITableViewCellSelectionStyleGray
    • UITableViewCellSelectionStyleDefault

创建 Cell 的性能分析及优化

cell的重用原理
  • iOS设备的内存有限,如果用UITableView显示成千上万条数据,就需要成千上万个UITableViewCell对象的话,那将会耗尽iOS设备的内存。要解决该问题,需要重用UITableViewCell对象
    • 重用原理:当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免创建新对象
  • 还有一个非常重要的问题:有时候需要自定义UITableViewCell(用一个子类继承UITableViewCell),而且每一行用的不一定是同一种UITableViewCell,所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的UITableViewCell,那么UITableView在重用UITableViewCell时可能会得到错误类型的UITableViewCell
    • 解决方案:UITableViewCell有个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化一个UITableViewCell对象
不优化时内存分析

不优化的时候

  • 离开可视范围就销毁
    • 频繁的开辟内存,销毁内存
代码语言:javascript
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];

    cell.textLabel.text = [NSString stringWithFormat:@"%zd行",indexPath.row];
    NSLog(@"cellForRowIndexPath --%zd",indexPath.row);
    return cell;
}
优化后内存地址分析

Cell的重用代码

代码语言:javascript
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.定义一个cell的标识
    //static 定义变量----只初始换一次
      static NSString *ID = @"jrcell";

    // 2.从缓存池中取出cell
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 3.如果缓存池中没有cell
      if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    //尽量将cell的初始化设置,放在这个代码块中
    //如果这个设置是所有cell都要保持一致的,就可以放在这个代码块中
    cell.textLabel.font = [UIFont systemFontOfSize:30];
    }

    // 4.设置cell的属性...

      return cell;
}

cell的重用代码新写法

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-06-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • UITableView
    • 介绍
      • tableView 常见属性
        • UITableView的两种样式只读属性
          • 展示数据
            • UITableView右边的索引条
              • tableView常用方法
                • UITableViewCell
                  • 创建 Cell 的性能分析及优化
                    • cell的重用原理
                    • 不优化时内存分析
                  • 不优化的时候
                    • 优化后内存地址分析
                  • Cell的重用代码
                    • cell的重用代码新写法
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档