UIday1101:UITableView 3 自定义cell 多种cell混合使用 cell自适应高度

来源:互联网 发布:软件研发部组织结构图 编辑:程序博客网 时间:2024/05/01 09:17
⼀、⾃定义 cell
 ⾃定义 cell 就是创建⼀个 UITableViewCell 的⼦类。
 把 cell 上的控件创建都封装在⼦类中,简化 UIViewController 中的代码
 ⼦视图控件添加到 cell 的 contentView 上。
 
 cell 中的控件如何显示 Model 中的信息?
 cell 中声明⼀个 Model 类型的属性, viewController 中获取到 Model 对 象后赋值给 cell 的 Model 属性,
 cell 中重写 Model 的 setter ⽅法,把 Model 对象中的内容重新赋值给各个控件
 M 和 V 不直接进⾏通信, C 负责 M 和 V 之间进⾏通信
 
 ⼆、多种类型的 CELL 混合使用
 开发中常⻅多种类型的 cell 混合在⼀个 tableView 中使⽤。
 通常每种类型的 cell 有不同的布局样式。
 
 注意事项:
 通常我们会在 tableView:cellForRowAtIndexPath: ⽅法中根据不同的 Model 来决定使⽤什么类型的 cell
 每种类型的 cell 要定义不同的重⽤标识符
 cell 重⽤的时候会根据重⽤标识从重⽤队列中取⽤哪种类型的 cell
 
 三、 CELL ⾃适应高度
 计算⼀段⽂本在限定宽⾼内所占矩形⼤小
 //iOS7 计算⽂本⾼度⽅法:
 - (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context;
 
 计算⽂本⾼度是所⽤的字体要和 label 显⽰时⽤的字体⼀致。
 label 的宽度要和计算时使⽤的限定宽度⼀致。
 这样才能保证文本显⽰在 label 中时, label ⾼度恰巧够。
 
 tableView:heightForRowAtIndexPath: ⽅法要比 tableView:cellForRowAtIndexPath 先执⾏。

 所以要提前计算好每⾏ cell 需要多少⾼度。

//cell只能设置高度,宽度是和tabView屏幕一样宽的-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 100;}


0 0