UITableViewCell

属性:

//设置右边的指示样式
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//设置右边的指示控件 cell.accessoryView
= [[UISwitch alloc] init];
//设置cell的选中样式 cell.selectionStyle
= UITableViewCellSelectionStyleNone;
//设置背景色 cell.backgroundColor
= [UIColor redColor];
//设置背景view
UIView
*bg = [[UIView alloc] init]; bg.backgroundColor = [UIColor blueColor]; cell.backgroundView = bg;
//设置选中的背景view UIView
*selectedBg = [[UIView alloc] init]; selectedBg.backgroundColor = [UIColor purpleColor]; cell.selectedBackgroundView = selectedBg;

性能优化:

  思路:当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当UITableView要求dataSource返回    UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免创建新对象。

  传统写法:

/**

 *  每当一个cell要进入视野范围就会调用这个方法

 */

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    // 1.定义一个重用标识

    static NSString *ID = @"wine";

    // 2.去缓存池取可循环利用的cell

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    

    // 3.缓存池如果没有可循环利用的cell,自己创建

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

        // 建议:所有cell都一样的设置,写在这个大括号中;所有cell不都一样的设置写在外面

       cell.backgroundColor = [UIColor redColor];

    }

    // 4.设置数据

    cell.textLabel.text = [NSString stringWithFormat:@"第%zd行数据",indexPath.row];

    

    return cell;

}

注册写法:

NSString *ID = @"wine";

- (void)viewDidLoad {

    [super viewDidLoad];

    // 注册ID 这个标识对应的cell类型为UITableViewCell

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    // 1.先去缓存池中查找可循环利用的cell

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 2.设置数据

    cell.textLabel.text = [NSString stringWithFormat:@"%zd行的数据", indexPath.row];

    return cell;

}

 

posted @ 2020-04-07 00:48  千面客  阅读(276)  评论(0编辑  收藏  举报