• iOS开发基础-UITableView基本属性


      设置 UITableView 中 cell 的背景颜色。

    示例1:通过 backgroundView 设置。

    1 UIView *view1 = [[UIView alloc] init];
    2 view1.backgroundColor = [UIColor blueColor];
    3 cell.backgroundView = view1;

    示例2:通过 backgroundColor 设置。

    1 cell.backgroundColor = [UIColor blueColor];

       backgroundView 的优先级比 backgroundColor 高,如果同时设置了, backgroundView 会覆盖 backgroundColor 。


      设置 cell 选中状态的背景。

    1 UIView *view2 = [[UIView alloc] init];
    2 view2.backgroundColor = [UIColor redColor];
    3 cell.selectedBackgroundView = view2;

      设置 UITableView 的其他属性。

    1 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;  //设置分割线样式
    2 self.tableView.separatorColor = [UIColor redColor]; //设置分割线颜色
    3 self.tableView.tableHeaderView = [UIButton buttonWithType:UIButtonTypeContactAdd];  //设置顶部视图
    4 self.tableView.tableFooterView = [[UISwitch alloc] init];   //设置底部视图

      设置 cell 的 accessoryType 属性。

       accessoryType 为枚举类型,定义如下:

    1 typedef enum : NSInteger {
    2    UITableViewCellAccessoryNone,
    3    UITableViewCellAccessoryDisclosureIndicator,
    4    UITableViewCellAccessoryDetailDisclosureButton,
    5    UITableViewCellAccessoryCheckmark,
    6    UITableViewCellAccessoryDetailButton 
    7 } UITableViewCellAccessoryType;

      也可通过 cell 的 accessoryView 属性来设置辅助指示视图,如下:

    1 cell.accessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];

       cell 的工作原理:在程序执行的时候,能看到多少行,就创建多少条数据。

      缺点:如果数据量非常大,用户在短时间内来回滚动,将会创建大量的 cell ,并不重用之前已经创建的 cell ,将一直开辟新的存储空间。


       cell 的重用原理:当滚动列表时,部分 UITableViewCell 会移出窗口, UITableView 会将窗口外的 UITableViewCell 放入一个对象池中等待重用。当 UITableView 要求 dataSource 返回 UITableViewCell 时, dataSource 会先查看该对象池,如果池中有未使用的 UITableViewCell ,则会用新的数据来配置这个 UITableViewCell ,然后返回给 UITableView ,并重新显示到窗口中,从而避免创建新对象。因此,如果一个窗口只能显示5个 cell ,重用之后,只需要创建6个 cell 。

      通过 UITableViewCell 的 reuseIdentifier 属性,可在初始化的时候传入一个特定的字符串标识符来设置。当 UITableView 要求 dataSource 返回 UITableViewCell 时,先通过该标识符到对象池中查找对应类型的 UITableViewCell 对象。如果没有,就传入这个字符串标识符初始化 UITableViewCell 对象。

     1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     2     NSLog(@"%s", __FUNCTION__);
     3     static NSString *identifier = @"hero";  //保存重用的标识符
     4     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];   //先去对象池中查找是否有满足条件的cell
     5     if (cell == nil) {
     6         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
     7         NSLog(@"创建一个新的Cell");
     8     }
     9     // 给cell设置数据
    10     
    11     return cell;
    12 }
  • 相关阅读:
    vue 路由跳转传参
    vue better-scroll 下拉上拉,加载刷新
    H5点击拨打电话,发短信
    vue搭建项目
    How to determine the socket connection up time on Linux
    @ContextConfiguration注解
    MySQL修改主键属性
    软件测试工程师面试(一)
    Python实现不同格式打印九九乘法表
    MySQL 5.1安装和配置过程中遇到的问题
  • 原文地址:https://www.cnblogs.com/wjq-Law/p/5221020.html
Copyright © 2020-2023  润新知