zoukankan      html  css  js  c++  java
  • UITableView中自定义等高Cell(分别用frame代码和xib描述Cell实现)

    1)纯代码
    新建一个继承自UITableViewCell的子类,比如KWWTgCell @interface KWWTgCell : UITableViewCell @end 在KWWTgCell.m文件中 重写-initWithStyle:reuseIdentifier:方法 在这个方法中添加所有的子控件 给子控件做一些初始化设置(设置字体、文字颜色等) /** * 在这个方法中添加所有的子控件 */ - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { // ...... } return self; } 重写-layoutSubviews方法 一定要调用[super layoutSubviews] 在这个方法中计算和设置所有子控件的frame /** * 在这个方法中计算所有子控件的frame */ - (void)layoutSubviews { [super layoutSubviews]; // ...... } 在KWWTgCell.h文件中提供一个模型属性,比如KWWTg模型 @class KWWTg; @interface KWWTgCell : UITableViewCell /** 团购模型数据 */ @property (nonatomic, strong) KWWTg *tg; @end 在KWWTgCell.m中重写模型属性的set方法 在set方法中给子控件设置模型数据 - (void)setTg:(KWWTg *)tg { _tg = tg; // ....... } 在控制器中 注册cell的类型 [self.tableView registerClass:[XMGTgCell class] forCellReuseIdentifier:ID]; 给cell传递模型数据 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 访问缓存池 KWWTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 设置数据(传递模型数据) cell.tg = self.tgs[indexPath.row]; return cell; }
    2)利用xib描述Cell

    新建一个继承自UITableViewCell的子类,比如KWWTgCell @interface KWWTgCell : UITableViewCell @end 新建一个xib文件(文件名最好跟类名一致,比如KWWTgCell.xib) 修改cell的class为KWWTgCell 绑定循环利用标识 添加子控件,设置子控件约束 将子控件连线到类扩展中 @interface KWWTgCell() @property (weak, nonatomic) IBOutlet UIImageView *iconImageView; @property (weak, nonatomic) IBOutlet UILabel *titleLabel; @property (weak, nonatomic) IBOutlet UILabel *priceLabel; @property (weak, nonatomic) IBOutlet UILabel *buyCountLabel; @end 在KWWTgCell.h文件中提供一个模型属性,比如KWWTg模型 @class KWWTg; @interface KWWTgCell : UITableViewCell /** 团购模型数据 */ @property (nonatomic, strong) KWWTg *tg; @end 在KWWTgCell.m中重写模型属性的set方法 在set方法中给子控件设置模型数据 - (void)setTg:(KWWTg *)tg { _tg = tg; // ....... } 在控制器中 注册xib文件 [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XMGTgCell class]) bundle:nil] forCellReuseIdentifier:ID]; 给cell传递模型数据 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 访问缓存池 KWWTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 设置数据(传递模型数据) cell.tg = self.tgs[indexPath.row]; return cell; }
  • 相关阅读:
    lucene .NET 搜索图片 功能实现
    (转)权威支持: 选择正确的 WebSphere 诊断工具
    (转)WebSphere 中池资源调优
    (转)使用 DB2 HADR 选择用于灾难恢复的 SUPERASYNC 模式
    (转) DB2 HADR
    (转)DB2 HADR 监控详解
    (转)python高级FTP
    (转)Python的web服务器
    (转)python通过paramiko实现,ssh功能
    (转)request模拟知乎登录(无验证码机制
  • 原文地址:https://www.cnblogs.com/kongweiiwei/p/4646765.html
Copyright © 2011-2022 走看看