现在的位置: 首页 > 综合 > 正文

UITableViewCell的层次结构

2017年08月17日 ⁄ 综合 ⁄ 共 1464字 ⁄ 字号 评论关闭

一直在用UITableViewCell,但是一直对它里面装的那些子控件的层次结构不十分的清楚,今天在网上找到了一段输出控件的层次结构的代码,博客地址:http://blog.csdn.net/hamasn/article/details/8216584,于是写了一个简单的Demo输出了一下cell的层次结构,cell的设置如下:

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
        cell.imageView.image = [UIImage imageNamed:@"search.png"];
        
        cell.textLabel.text = [NSString stringWithFormat:@"cell_%ld", (long)indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDetailButton;
        
        // cell.backgroundView默认为nil
        UIView *view = [[UIView alloc] initWithFrame:cell.frame];
        view.backgroundColor = [UIColor yellowColor];
        cell.backgroundView = view;
        
        if (indexPath.row == 0) {
            NSLog(@"The view tree:\n%@", [self displayViews:cell]);
        }

输出如下:

2014-02-10 17:28:14.764 TestTableViewCell[1489:a0b] The view tree:

[ 0] UITableViewCell

--[ 1] UITableViewCellScrollView

----[ 2] UIView   ---------backgroundView 默认是nil的

----[ 2] UITableViewCellContentView

------[ 3] UIImageView

------[ 3] UILabel

----[ 2] UIButton --------那个accessoryDetailButton,与contentView是一层的


==============顺便看一下UITableView的delegate和dataSource中的方法的执行顺序==========

1、tableView: numberOfRowsInSection:

2、tableView: heightForRowAtIndexPath:

3、tableView: cellForRowAtIndexPath:

想一想自己之前的代码,居然在tableView: heightForRowAtIndexPath:中调用tableView: cellForRowAtIndexPath:,然后返回cell的高度,tableView: heightForRowAtIndexPath:是每刷新一次每一行都会请求一次,不管cell当前是否显示,这样每次刷新都多执行了rowCount个tableView: cellForRowAtIndexPath:方法,极大的浪费性能啊!!!

还是得把cell的高度计算好存储起来,在cocoachina上看到一篇文章提到的建议比较好,将cell高度的计算放在异步请求拉回数据后就计算,这样就不会因为计算行高耗时而使tableview卡。

抱歉!评论已关闭.