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;
    
    }
  • 相关阅读:
    cdoj1325卿学姐与基本法
    HUAS 1476 不等数列(DP)
    BZOJ 1818 内部白点(离散化+树状数组)
    BZOJ 1816 扑克牌(二分)
    BZOJ 1801 中国象棋(DP)
    BZOJ 1791 岛屿(环套树+单调队列DP)
    BZOJ 1797 最小割(最小割割边唯一性判定)
    BZOJ 1789 Y形项链(思维)
    BZOJ 1787 紧急集合(LCA)
    BZOJ 1786 配对(DP)
  • 原文地址:https://www.cnblogs.com/wwjwb/p/12650785.html
Copyright © 2011-2022 走看看