美文网首页ios杂谈
自定义等高的cell

自定义等高的cell

作者: iOS_Cqlee | 来源:发表于2015-09-27 00:31 被阅读136次

自定义等高的cell

等高的cell

所有cell的高度是一样的


纯代码创建

frame

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

@interfaceTgCell:UITableViewCell@end

2,在TgCell.m文件中

重写-initWithStyle:reuseIdentifier:方法

在这个方法中添加所有需要显示的子控件

给子控件做一些初始化设置(设置字体、文字颜色等)

/**

*  在这个方法中添加所有的子控件

*/- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier{if(self= [superinitWithStyle:style reuseIdentifier:reuseIdentifier]) {// ......}returnself;}

重写-layoutSubviews方法

一定要调用[super layoutSubviews]

在这个方法中计算和设置所有子控件的frame

/**

*  在这个方法中计算所有子控件的frame

*/- (void)layoutSubviews{    [superlayoutSubviews];// ......}

3,在TgCell.h文件中提供一个模型属性,比如Tg模型

@classTg;@interfaceTgCell:UITableViewCell/** 团购模型数据 */@property(nonatomic,strong) Tg *tg;@end

4,在TgCell.m中重写模型属性的set方法

在set方法中给子控件设置模型数据

- (void)setTg:(Tg *)tg{    _tg = tg;// .......}

5,在控制器中

注册cell的类型

[self.tableViewregisterClass:[TgCell class] forCellReuseIdentifier:ID];

给cell传递模型数据

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{// 访问缓存池TgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];// 设置数据(传递模型数据)cell.tg=self.tgs[indexPath.row];returncell;}


Autolayout

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

@interfaceTgCell:UITableViewCell@end

2,在TgCell.m文件中

重写-initWithStyle:reuseIdentifier:方法

在这个方法中添加所有需要显示的子控件

给子控件做一些初始化设置(设置字体、文字颜色等)

添加子控件的完整约束

/**

*  在这个方法中添加所有的子控件

*/- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier{if(self= [superinitWithStyle:style reuseIdentifier:reuseIdentifier]) {// ......}returnself;}

3在模型Cell.h文件中提供一个模型属性,比如Tg模型

@classTg;@interfaceTgCell:UITableViewCell/** 团购模型数据 */@property(nonatomic,strong) Tg *tg;@end

4,在TgCell.m中重写模型属性的set方法

在set方法中给子控件设置模型数据

- (void)setTg:(Tg *)tg{    _tg = tg;// .......}

5,在控制器中

注册cell的类型

[self.tableViewregisterClass:[TgCell class] forCellReuseIdentifier:ID];

给cell传递模型数据

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{// 访问缓存池TgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];// 设置数据(传递模型数据)cell.tg=self.tgs[indexPath.row];returncell;}


XIB

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

@interfaceTgCell:UITableViewCell@end

新建一个xib文件(文件名最好跟类名一致,比如TgCell.xib)

修改cell的class为TgCell

绑定循环利用标识

添加子控件,设置子控件约束

将子控件连线到类扩展中

@interfaceTgCell()@property(weak,nonatomic)IBOutletUIImageView*iconImageView;@property(weak,nonatomic)IBOutletUILabel*titleLabel;@property(weak,nonatomic)IBOutletUILabel*priceLabel;@property(weak,nonatomic)IBOutletUILabel*buyCountLabel;@end

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

@classTg;@interfaceTgCell:UITableViewCell/** 团购模型数据 */@property(nonatomic,strong) Tg *tg;@end

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

在set方法中给子控件设置模型数据

- (void)setTg:(Tg *)tg{    _tg = tg;// .......}

在控制器中

注册xib文件

[self.tableViewregisterNib:[UINibnibWithNibName:NSStringFromClass([XMGTgCell class]) bundle:nil] forCellReuseIdentifier:ID];

给cell传递模型数据

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{// 访问缓存池TgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];// 设置数据(传递模型数据)cell.tg=self.tgs[indexPath.row];returncell;}


storyBoard

基本上与XIB一致,当在缓存池中没有找到数据,会自动去storyboard当中去寻找,注意在要设置 identify否则在缓存池中会找不到这数据

相关文章

  • 自定义不等高cell

    自定义不等高cell 自定义不等高cell(纯代码) 给模型增加frame数据 所有子控件的frame cell...

  • 02-UITableview(3)

    0605非自定义等高 表格刷新 1. 01-自定义非等高cell01-xib 用故事板(sb)创建的cell既不用...

  • 自定义 tableView 的 cell

    - 自定义等高的 cell 一、storyboard 自定义 cell 1.创建一个继承自 UITableView...

  • cell的等高与不等高

    自定义等高的cell 等高的cell 所有cell的高度是一样的 纯代码创建 frame 1,新建一个继承自UIT...

  • 自定义等高的cell

    自定义等高的cell 等高的cell 所有cell的高度是一样的 纯代码创建 frame 1,新建一个继承自UIT...

  • UITableViewCell根据内容显示不同的高度问题

    1.iOS8及以后,通过XIb自定义不等高cell: 下载 // 告诉tableView所有cell的真实高...

  • 5.等高的cell设置

    1.内容大纲: 2.纯代码,添加子控件 项目初始准备: 创建自定义的cell: 3.自定义等高的cell--xib...

  • 等高Cell

    一、自定义Cell1、等高cell 代码 很古老的方法: 利用autoLayout xib加载xib要通过手动加载...

  • UITableView(自定义cell)(2)

    自定义不等高cell frame方法 给模型增加frame数据 所有子控件的frame cell的高度 重写模型c...

  • UITableView(自定义cell)(1)

    自定义等高cell frame方法 在XMGTgCell.m文件中 重写-initWithStyle:reuseI...

网友评论

    本文标题:自定义等高的cell

    本文链接:https://www.haomeiwen.com/subject/yvjwcttx.html