博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS-UI控件之UITableView(二)- 自定义不等高的cell
阅读量:4588 次
发布时间:2019-06-09

本文共 1883 字,大约阅读时间需要 6 分钟。

不等高的cell

给模型增加frame数据

  • 所有子控件的frame
  • cell的高度
@interface XMGStatus : NSObject/**** 文字\图片数据 ****/ // ..... /**** frame数据 ****/ /** 头像的frame */ @property (nonatomic, assign) CGRect iconFrame; // ..... /** cell的高度 */ @property (nonatomic, assign) CGFloat cellHeight; @end
  • 重写模型cellHeight属性的get方法
- (CGFloat)cellHeight{    if (_cellHeight == 0) {        // ... 计算所有子控件的frame、cell的高度    }    return _cellHeight; }

在控制器中

  • 实现一个返回cell高度的代理方法
    • 在这个方法中返回indexPath位置对应cell的高度
/** *  返回每一行cell的具体高度 */- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    XMGStatus *status = self.statuses[indexPath.row]; return status.cellHeight; }
  • 给cell传递模型数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *ID = @"tg"; // 访问缓存池 XMGStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 设置数据(传递模型数据) cell.status = self.statuses[indexPath.row]; return cell; }

新建一个继承自UITableViewCell的子类,比如XMGStatusCell

@interface XMGStatusCell : UITableViewCell@end

在XMGStatusCell.m文件中

  • 重写-initWithStyle:reuseIdentifier:方法
    • 在这个方法中添加所有需要显示的子控件
    • 给子控件做一些初始化设置(设置字体、文字颜色等)
/** *  在这个方法中添加所有的子控件 */- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { // ...... } return self; }

在XMGStatusCell.h文件中提供一个模型属性,比如XMGTg模型

@class XMGStatus;@interface XMGStatusCell : UITableViewCell /** 团购模型数据 */ @property (nonatomic, strong) XMGStatus *status; @end

在XMGTgCell.m中重写模型属性的set方法

  • 在set方法中给子控件设置模型数据
- (void)setStatus:(XMGStatus *)status{    _status = status;    // .......}

重写-layoutSubviews方法

  • 一定要调用[super layoutSubviews]
  • 在这个方法中设置所有子控件的frame
/** *  在这个方法中设置所有子控件的frame */- (void)layoutSubviews{    [super layoutSubviews];    // ......}

转载于:https://www.cnblogs.com/ShaoYinling/p/4609872.html

你可能感兴趣的文章
[转]社会生活中十二大著名法则 1 马太效应 2 手表定理 3 不值得定律 4 彼得原理 5 零和游戏原理 6 华盛顿合作规律 7 酒与污水定律 8 水桶定律 9 蘑菇管理 10 奥...
查看>>
浅谈三层与实体
查看>>
cocostudio——js 3 final控件事件
查看>>
Flex 学习笔记 datatip的背景颜色
查看>>
iOS开发中六种手势识别
查看>>
oracle创建临时表没有权限
查看>>
76.数塔问题
查看>>
PHP 透明水印生成代码
查看>>
我就是学习
查看>>
Hibernate的缓存
查看>>
(十五)Struts2 本地化/国际化(i18n)
查看>>
利用Qt Phonon框架制作音视频播放器
查看>>
Bundle savedInstanceState的作用
查看>>
打印图片,自动调整宽高
查看>>
对类使用dir()
查看>>
【13】淘宝sdk——入门实战之header.php制作(一)
查看>>
安装SoapUI Pro
查看>>
杜教BM模板
查看>>
Makefile经典教程(掌握这些足够)
查看>>
自己成功的编写的将数据从excel导入到access中
查看>>