美文网首页iOS
UITableView中的一些注意点

UITableView中的一些注意点

作者: coderJerry | 来源:发表于2015-06-22 21:09 被阅读1746次

    性能优化

    • 内存的循环引用机制:
      每当需要一个模型数据就创建一个内存给它,这种做法太耗内存,所以我们利用循环引用的方法来提高效率,降低损耗。
      这里的循环引用简单来说,就是将已经不在显示区域显示的数据的内存地址,先放入缓存池,当有下一个数据要显示时,再将原来放入释放池的内存空间给该数据。这样做到了重复利用,提高了内存的利用效率。
      不过当数据量较大时,为了不让数据到缓存池中乱拿内存空间,我们需要将同一类的内存空间写上标示符,这样,同一类的数据就不会拿错内存空间。
      原理图如下图所示:


      内存循环引用机制
    • 性能优化的实例

    /**
     *  什么时候调用:每当有一个cell进入视野范围内就会调用
     */
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // 重用标识
        // 被static修饰的局部变量:只会初始化一次,在整个程序运行过程中,只有一份内存
        static NSString *ID = @"cell";
    
        // 1.先根据cell的标识去缓存池中查找可循环利用的cell
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
        // 2.如果cell为nil(缓存池找不到对应的cell)
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
            //NSLog(@"%p--------", cell);
        }
    
        // 3.覆盖数据
        cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];
    
        NSLog(@"%p--------", cell);
    
        return cell;
    }
    
    • 特别提醒:程序中只写了dequeueReusableCellWithIdentifier的重用标识,当我们是用storyboard或者xib来创建cell时,那么必须在storyboard或者xib右边storyboard属性设置框中将identifier重用标识与代码中的标识设置一致才行。不然就不能实现循环引用。

    重用标识的使用dequeue--出列,离队的意思

    • 释放池中创建重用标识
      • 方法1:没有的话,就自己创建
    // 1.如果cell为nil(缓存池找不到对应的cell)
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
            //NSLog(@"%p--------", cell);
        }
    
    - 方法2:在ViewDidLoad中注册
    
    // 2.注册某个标识对应的cell类型
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
    
    - 方法3:在storyboard中右边框中填写identifier
    

    这三种方法都可以让释放池中创建出一个想要的重用标识,选择其一即可。

    UITableViewDelegate中的一些常用方法

    • 点击某个cell时就调用
    //点击某个cell的时候调用
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        ...
    }
    
    • 取消选中某一行时调用
    //取消选中某一行的时候调用
    - (void)tableView:(UITableView *)tableView
    didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
    
    • 设置tableView第indexPath行cell的高度
      这个方法与self.tableView.rowHeight有所不同,rowHeight是每一行的行高都相同,而heightForRow会根据不同的row计算可能不同的值。但在执行效率上,rowHeight会比heightForRow方法高,因为heightForRow代理方法一直在计算高度,计算完以后再创建cell。
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    
    • 补充:
    • UITableView的2种样式
    typedef enum {
       UITableViewStylePlain,//没有分section组
       UITableViewStyleGrouped//有分section组
    } UITableViewStyle;
    

    例如:
    创建tableView时定义样式:

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    
    • 设置tableView第section显示的头部控件

    不仅仅可以返回文字标题,还可以返回图片

    //告诉tableView第section显示怎样的头部控件
    - (UIView *)tableView:(UITableView *)tableView
    viewForHeaderInSection:(NSInteger)section
    
    • 设置tableView第section显示的脚部控件
    //告诉tableView第section显示怎样的脚部控件
    - (UIView *)tableView:(UITableView *)tableView
    viewForFooterInSection:(NSInteger)section
    
    • 特别提醒

      • 在storyboard或者xib中新建一个框架(UIView、UITableView、UITableViewCell等等)时,请将它们的class与你所要联系的类名统一起来,千万不要忘记~
      • 在UITableViewController中self.view和self.tableView是指同一个view。
      • storyboard和xib的UITableViewCell控件中,尽量写上identifier。

    设置分隔线

    self.tableView.separatorColor = [UIColor redColor];
    self.tableView.separatorEffect = ...;
    self.tableView.separatorInset = ...;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    ...(自己可以查看API)
    

    设置选中样式

    • 选中样式有以下几种
    typedef enum : NSInteger {
       UITableViewCellSelectionStyleNone,//没有样式
       UITableViewCellSelectionStyleBlue,//蓝色
       UITableViewCellSelectionStyleGray,//灰色
       UITableViewCellSelectionStyleDefault//默认样式
    } UITableViewCellSelectionStyle;
    
    //设置为没有样式
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    不过要注意的是,虽然是没有样式的,但是点击还是会有作用的。(即如果有didSelectRowAtIndexPath:,点击了还是会调用的)

    设置选中的背景颜色

    //设置选中的背景色
    UIView *selectedBackgroundView = [[UIView alloc] init];
    selectedBackgroundView.backgroundColor = [UIColor redColor];
    //设置默认的背景色
    cell.backgroundColor = [UIColor blueColor];
    //设置默认的背景色,这个背景色的优先级高于backgroundColor
    UIView *backgroundView = [[UIView alloc] init];
    backgroundView.backgroundColor = [UIColor greenColor];
    cell.backgroundView = backgroundView;
    

    设置指示器

    // 设置指示器
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//小箭头
    cell.accessoryType = UITableViewCellAccessoryCheckmark;//打钩
    cell.accessoryType = UITableViewCellAccessoryDetailButton;//感叹号提示
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//感叹号+小箭头
    cell.accessoryType = UITableViewCellAccessoryNone;//啥都没有
    

    同样的accessory指示器也可以添加其他的控件

    //添加一个开关指示器
    cell.accessoryView = [[UISwitch alloc] init];
    

    KVC

    KVC-Key Value Coding(键值编码)

    //KVC,就是可以自动转为deal.title = dict[@"title"];
    [deal setValuesForKeysWithDictionary:dict];
    

    tag的用法

    tag虽然好用,但请不要过度依赖哦~
    因为当项目大了以后,我们设置的tag如果很多的话,那tag的编号会很混乱,每次要用到了就要找tag的编号,这样的效率是很低的。所以能不用就不用。

    //设置代理,并调用代理方法
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        //设置重用标识
        static NSString *ID = @"deal";
        //记得把storyboard或xib右边的identifier填入deal
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
        // 取出模型数据
        XMGDeal *deal = self.deals[indexPath.row];
    
        // 设置数据
        UIImageView *iconView = (UIImageView *)[cell viewWithTag:10];
        iconView.image = [UIImage imageNamed:deal.icon];
    
        UILabel *titleLabel = (UILabel *)[cell viewWithTag:20];
        titleLabel.text = deal.title;
    
        UILabel *priceLabel = (UILabel *)[cell viewWithTag:30];
        priceLabel.text = [NSString stringWithFormat:@"¥%@", deal.price];
    
        UILabel *buyCountLabel = (UILabel *)[cell viewWithTag:40];
        buyCountLabel.text = [NSString stringWithFormat:@"%@人已购买", deal.buyCount];
        return cell;
    }
    

    估计高度的作用

    • 当我们用创建cell的数据源方法时,其实编译器不是按照我们写程序的顺序(先创建cell再定cell的高度),而是先多次根据内容的大小来计算高度,将高度确定后才会将cell创建出来,所以会不准确。
      这时候,我们用估计高度的方法,先让编译器知道有一个大致的高度值,目的是让编译器知道已经确定了cell的高度,紧接着就创建出cell,这时我们再调用高度具体的计算方法,从而得出较为准确的高度。这样同时可以提高编译器的性能。</br>
      下面举一个估计高度的应用场景:</br>
     #pragma mark - <UITableViewDataSource>
    // tableView的行数
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     {
        return self.messages.count;
     }
    // 创建cell
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {
        XMGMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"message”];
        cell.message = self.messages[indexPath.row];
        return cell;
     }
    
     #pragma mark - <UITableViewDelegate>
    
    // 设置估计高度
    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
     {
        return 200;
     }
    // 创建出cell后再计算出具体的行高
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
     {
        XMGMessage *message = self.messages[indexPath.row];
        return message.cellHeight;
     }
    

    获取最大布局的宽度

    • preferredMaxLayoutWidth</br>

    这个属性会在布局约束时影响label的宽度的最大值,如果一个文字超出了屏幕的最大实际宽度,那么这个文字就会流动到下一行,这样就增加了label的高度。
    利用了这个方法以后,我们可以更加精确的得到label的实际高度,以便我们更准确的布局。所以这个方法一般用于高度计算出现小偏差的时候。

    self.contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;
    

    注:文中若有错误,请及时和我交流,感激不尽~~

    相关文章

      网友评论

        本文标题:UITableView中的一些注意点

        本文链接:https://www.haomeiwen.com/subject/jnosqttx.html