iOS开发iOS开发技术分享iOS Developer

iOS学习笔记9 tableview学习

2016-04-10  本文已影响313人  蠢萌的L君

tableview的学习

1.每个cell对应一个模型
2.每个section对应3个模型,头部,底部,cell模型

每个tableview中申明一个可变数组存储每个模型数据.
数据模型写类方法,将字典转为模型

tableview的性能优化

// 由于优化性能,避免NSString *ID = @"cell"重复创建
NSString *ID = @"cell  

// 所以加static NSString *ID = @"cell"
static NSString *ID = @"cell"

// 被static修饰的局部变量,只会初始化一次,在整个程序运行过程中,只有一份内存.

1 或者在viewDidLoad中通过tableview注册cell

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

第三种方式创建tableview

// tableview常见设置
// 设置分割线颜色
self.tableview.separator = [UIColor redColor]
// 设置cell之间没有分割线
self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;

// tableviewCell常见设置
// 设置cell的点击效果
cell.selectionStyle = UITableViewCellSelectionStyleNo
// 设置选中背景色
cell.selectedBackgroundView 
// 设置默认的背景色
cell.backgroundColor
cell.backgroundView    优先级高于Color
// 设置指示器
cell.accessoryType
cell.accessoryView

自定义cell

  1. 等高Cell
  1. 非等高Cell

cell的调用

// 这一句返回cell估计高度,优化性能,让cell不会一次计算完所有高度,如果没有这句,cell会一次计算完所有高度
estimateHeightForRowAtIndexPath

1.estimateHeightForRowAtIndexPath 2.heightForRowAtIndexPath 3.cellForRowAtIndexPath

在自定义cell中

- (void)awakeFromNib
{
    // 设置label每一行文字的最大宽度
    // 为了保证计算出来的数值 跟 真正显示出来的效果 一致
    self.contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;
}
// 强制布局
[self layoutIfNeeded];
    
// 计算cell的高度
if (self.pictureView.hidden) { // 没有配图
        status.cellHeight = CGRectGetMaxY(self.contentLabel.frame) + 10;
    } else { // 有配图
        status.cellHeight = CGRectGetMaxY(self.pictureView.frame) + 10;
    }
上一篇 下一篇

猜你喜欢

热点阅读