zoukankan      html  css  js  c++  java
  • 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;
    
    }
  • 相关阅读:
    如何关闭微软反恶意软件客户端
    年轻爸爸家长会上猝死,事前已连续发烧三天
    人猝死前身体发出两个救命信号,一定要清楚,关键时刻救命
    火遍世界的视力恢复法 现在知道还不晚!
    重视工龄工资,食堂一定自己办
    VR行业纷纷倒闭:有硬件没内容
    企业为什么要去美国建厂
    bzoj 1046 : [HAOI2007]上升序列 dp
    bzoj 1857: [Scoi2010]传送带 三分
    bzoj 1045: [HAOI2008] 糖果传递
  • 原文地址:https://www.cnblogs.com/wwjwb/p/12650785.html
Copyright © 2011-2022 走看看