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 }

 

转载于:https://www.cnblogs.com/wjq-Law/p/5221020.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值