自定义等高 Cell
1、介绍
1.1 代码自定义 cell(使用 frame)
- 创建一个继承自 UITableViewCell 的子类,比如 BookCell1。
- 在 initWithStyle:reuseIdentifier: 方法中。
- 添加子控件。
- 设置子控件的初始化属性(比如文字颜色、字体)。
- 在 layoutSubviews 方法中设置子控件的 frame。
- 需要提供一个模型属性,重写模型的 set 方法,在这个方法中设置模型数据到子控件。
- 在 initWithStyle:reuseIdentifier: 方法中。
- 在控制器中
- 利用 registerClass... 方法注册 BookCell1 类。
- 利用重用标识找到 cell(如果没有注册类,就需要手动创建 cell)。
- 给 cell 传递模型数据。
- 也可以将创建获得 cell 的代码封装起来(比如 cellWithTableView: 方法)。
- 创建一个继承自 UITableViewCell 的子类,比如 BookCell1。
1.2 代码自定义 cell(使用 autolayout)
- 创建一个继承自 UITableViewCell 的子类,比如 BookCell1。
- 在 initWithStyle:reuseIdentifier: 方法中。
- 添加子控件。
- 添加子控件的约束(建议使用 Masonry)。
- 设置子控件的初始化属性(比如文字颜色、字体)。
- 需要提供一个模型属性,重写模型的 set 方法,在这个方法中设置模型数据到子控件。
- 在 initWithStyle:reuseIdentifier: 方法中。
- 在控制器中
- 利用 registerClass... 方法注册 BookCell1 类。
- 利用重用标识找到 cell(如果没有注册类,就需要手动创建 cell)。
- 给 cell 传递模型数据。
- 也可以将创建获得 cell 的代码封装起来(比如 cellWithTableView: 方法)。
- 创建一个继承自 UITableViewCell 的子类,比如 BookCell1。
2、代码创建 cell - frame
2.1 BookCell1.h
@class BookModel;
@interface BookCell1 : UITableViewCell
// 定义 Cell 的数据模型
@property (nonatomic, retain)BookModel *book;
@end
2.2 BookCell1.m
#import "BookCell1.h"
#import "BookModel.h"
@interface BookCell1()
// 创建自定义 Cell 视图包含的内容
@property(nonatomic, retain)UIImageView *iconView;
@property(nonatomic, retain)UILabel *titleLabel;
@property(nonatomic, retain)UILabel *detailLabel;
@property(nonatomic, retain)UILabel *priceLabel;
@end
@implementation BookCell1
// 重写初 Cell 始化方法,创建自定义 Cell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// 创建子视图
// 创建 iconView 视图,并添加到自定义 Cell 上
self.iconView = [[UIImageView alloc] init];
self.iconView.layer.borderColor = [[UIColor greenColor] CGColor];
self.iconView.layer.borderWidth = 2;
[self.contentView addSubview:self.iconView];
// 创建 titleLabel 视图
self.titleLabel = [[UILabel alloc] init];
self.titleLabel.font = [UIFont boldSystemFontOfSize:14];
self.titleLabel.textColor = [UIColor redColor];
[self.contentView addSubview:self.titleLabel];
// 创建 detailLabel 视图
self.detailLabel = [[UILabel alloc] init];
self.detailLabel.font = [UIFont systemFontOfSize:12];
[self.contentView addSubview:self.detailLabel];
// 创建 priceLabel 视图
self.priceLabel = [[UILabel alloc] init];
self.priceLabel.font = [UIFont systemFontOfSize:12];
[self.contentView addSubview:self.priceLabel];
}
return self;
}
// 布局子视图
- (void)layoutSubviews {
[super layoutSubviews];
// 布局子视图
self.iconView.frame = CGRectMake(10, 10, 60, 60);
self.titleLabel.frame = CGRectMake(90, 5, 200, 25);
self.detailLabel.frame = CGRectMake(90, 30, 200, 25);
self.priceLabel.frame = CGRectMake(90, 55, 200, 25);
}
// 设置显示的数据
- (void)setBook:(BookModel *)book {
_book = book;
// 设置数据,设置 cell 视图上显示的内容 内容
self.iconView.image = [UIImage imageNamed:book.icon];
self.titleLabel.text = book.title;
self.detailLabel.text = book.detail;
self.priceLabel.text = book.price;
}
@end
2.3 ViewController.m
// 使用自定义 Cell 创建,UITableViewDataSource 协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 使用自定义的 Cell 定义
BookCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"testIdentifier"];
if (cell == nil) {
// 使用自定义的 Cell 创建
cell = [[BookCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"testIdentifier"];
}
BookModel *bookModel = [self.myDataArray objectAtIndex:indexPath.row];
cell.book = bookModel;
return cell;
}
3、代码注册 cell - frame
- 在 tableView 创建时,从 iOS7 开始多了一种创建 cell 的方式(注册),让 tableView 注册一种 cell,需要设置复用标志。
- 用注册方式创建 cell,如果 tableView 已经注册了某一种 cell,从复用队列里查找,如果找不到,
- 系统会自动通过注册的 cell 类来创建 cell 对象。
3.1 自定义 Cell 部分同上。
- BookCell1.h
- BookCell1.m
3.2 ViewController.m
// 注册 cell
[myTableView registerClass:[BookCell1 class] forCellReuseIdentifier:@"testIdentifier"];
// 使用注册的 Cell 创建,UITableViewDataSource 协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BookCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"testIdentifier" forIndexPath:indexPath];
BookModel *bookModel = [self.myDataArray objectAtIndex:indexPath.row];
cell.book = bookModel;
return cell;
}
4、代码创建 cell - autolayout
- 第三方框架 Masonry Github 网址
4.1 XMGDeal.h
@interface XMGDeal : NSObject
@property (strong, nonatomic) NSString *buyCount;
@property (strong, nonatomic) NSString *price;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *icon;
+ (instancetype)dealWithDict:(NSDictionary *)dict;
@end
4.2 XMGDeal.m
@implementation XMGDeal
+ (instancetype)dealWithDict:(NSDictionary *)dict {
XMGDeal *deal = [[self alloc] init];
// KVC - Key Value Coding
[deal setValuesForKeysWithDictionary:dict];
return deal;
}
@end
4.3 XMGDealCell.h
@class XMGDeal;
@interface XMGDealCell : UITableViewCell
/** 模型数据 */
@property (nonatomic, strong) XMGDeal *deal;
+ (instancetype)cellWithTableView:(UITableView *)tableView;
@end
4.4 XMGDealCell.m
#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS
#import "Masonry.h"
@interface XMGDealCell()
@property (weak, nonatomic) UIImageView *iconView;
@property (weak, nonatomic) UILabel *titleLabel;
@property (weak, nonatomic) UILabel *priceLabel;
@property (weak, nonatomic) UILabel *buyCountLabel;
@end
@implementation XMGDealCell
+ (instancetype)cellWithTableView:(UITableView *)tableView {
static NSString *ID = @"deal";
// 创建cell
XMGDealCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[XMGDealCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
return cell;
}
// 1.在 initWithStyle:reuseIdentifier: 方法中添加子控件
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
UIImageView *iconView = [[UIImageView alloc] init];
[self.contentView addSubview:iconView];
self.iconView = iconView;
UILabel *titleLabel = [[UILabel alloc] init];
[self.contentView addSubview:titleLabel];
self.titleLabel = titleLabel;
UILabel *priceLabel = [[UILabel alloc] init];
priceLabel.textColor = [UIColor orangeColor];
[self.contentView addSubview:priceLabel];
self.priceLabel = priceLabel;
UILabel *buyCountLabel = [[UILabel alloc] init];
buyCountLabel.textAlignment = NSTextAlignmentRight;
buyCountLabel.font = [UIFont systemFontOfSize:14];
buyCountLabel.textColor = [UIColor lightGrayColor];
[self.contentView addSubview:buyCountLabel];
self.buyCountLabel = buyCountLabel;
}
return self;
}
// 2.在 layoutSubviews 方法中设置子控件的 约束
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat margin = 10;
[self.iconView makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(100);
make.left.top.offset(margin);
make.bottom.offset(-margin);
}];
[self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.iconView);
make.left.equalTo(self.iconView.right).offset(margin);
make.right.offset(-margin);
}];
[self.priceLabel makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.titleLabel);
make.bottom.equalTo(self.iconView);
make.width.equalTo(70);
}];
[self.buyCountLabel makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.priceLabel);
make.right.equalTo(self.titleLabel);
make.left.equalTo(self.priceLabel.right).offset(margin);
}];
}
// 3.重写模型的 set 方法
- (void)setDeal:(XMGDeal *)deal {
_deal = deal;
// 设置数据
self.iconView.image = [UIImage imageNamed:deal.icon];
self.titleLabel.text = deal.title;
self.priceLabel.text = [NSString stringWithFormat:@"¥%@", deal.price];
self.buyCountLabel.text = [NSString stringWithFormat:@"%@人已购买", deal.buyCount];
}
@end
4.5 XMGDealsViewController.m
@interface XMGDealsViewController ()
/** 所有的团购数据 */
@property (nonatomic, strong) NSArray *deals;
@end
@implementation XMGDealsViewController
- (NSArray *)deals {
if (_deals == nil) {
// 加载plist中的字典数组
NSString *path = [[NSBundle mainBundle] pathForResource:@"deals.plist" ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
// 字典数组 -> 模型数组
NSMutableArray *dealArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
XMGDeal *deal = [XMGDeal dealWithDict:dict];
[dealArray addObject:deal];
}
_deals = dealArray;
}
return _deals;
}
- (void)viewDidLoad {
[super viewDidLoad];
// [self.tableView registerClass:[XMGDealCell class] forCellReuseIdentifier:@"deal"];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.deals.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//创建cell
XMGDealCell *cell = [XMGDealCell cellWithTableView:tableView];
// 取出模型数据
cell.deal = self.deals[indexPath.row];
return cell;
}
@end
自定义等高 Cell的更多相关文章
- 自定义不等高cell—storyBoard或xib自定义不等高cell
1.iOS8之后利用storyBoard或者xib自定义不等高cell: 对比自定义等高cell,需要几个额外的步骤(iOS8开始才支持) 添加子控件和contentView(cell的content ...
- iOS开发——UI进阶篇(三)自定义不等高cell,如何拿到cell的行高,自动计算cell高度,(有配图,无配图)微博案例
一.纯代码自定义不等高cell 废话不多说,直接来看下面这个例子先来看下微博的最终效果 首先创建一个继承UITableViewController的控制器@interface ViewControll ...
- iOS开发——UI进阶篇(二)自定义等高cell,xib自定义等高的cell,Autolayout布局子控件,团购案例
一.纯代码自定义等高cell 首先创建一个继承UITableViewCell的类@interface XMGTgCell : UITableViewCell在该类中依次做一下操作1.添加子控件 - ( ...
- 纯代码自定义不等高cell
数据模型.plist解析这里就不过多赘述. 错误思路之一: 通过在heightForRowAtIndexPath:方法中调用cellForRowAtIndexPath:拿到cell,再拿到cell的子 ...
- iOS-UI控件之UITableView(三)- 自定义不等高的cell
Storyboard_不等高 对比自定义等高cell,需要几个额外的步骤(iOS8开始才支持) 添加子控件和contentView之间的间距约束 设置tableViewCell的真实行高和估算行高 / ...
- 自定义不等高的cell-(storyboard)
对比自定义等高cell,需要几个额外的步骤(iOS8开始才支持) 添加子控件和contentView之间的间距约束 设置tableViewCell的真实行高和估算行高 // 告诉tableView所有 ...
- 自定义非等高 Cell
1.自定义非等高 Cell介绍 1.1 代码自定义(frame) 新建一个继承自 UITableViewCell 的类. 重写 initWithStyle:reuseIdentifier: 方法. 添 ...
- 不等高cell的tableView界面搭建
一.搭建界面 1.界面分析 分析界面的层次结构,分析界面应该用什么控件来搭建 2.界面层次结构 分析之后,我们可以把这个界面分为四个模块(topView middleView commentView ...
- 不等高cell搭建(二)
一.commentView模块搭建 commentView样式分为两种 1.xib搭建界面 1.1 因为评论的样式大体上一样,我们可以用同一个xib来处理 1.2 最热评论 用 ...
随机推荐
- [转载]rmmod: can't change directory to '/lib/modules': No such file or directory
转载网址:http://blog.csdn.net/chengwen816/article/details/8781096 在我新移植的kernel(3.4.2)和yaffs2文件中,加载新编译的内核 ...
- HZ 和 usleep最小睡眠时间(低精度定时器)
注:本文论述的情况是内核默认未开启高精度定时器. 不建议这么用,需要较高精度的定时器可参考本博客后面的文章. 一.先上结论 1.默认的HZ是100,usleep的最小时间是10ms: 2.将HZ修改为 ...
- Day1--Python基础1--上半部分
一.第一个python程序 在linux下创建一个文件叫做hello.py,并输入 print "Hello World" 然后执行命令:python hello.py,输出 [r ...
- java从键盘输入数,分解质因数,
总结:1.break;的用法 当最小质因数不能被输入的值整除时,需要继续循环.k++. 当然输入的数,本身就是质数时,那么 package com.b; import java.util.Scanne ...
- HTML5的离线应用
参考:有趣的HTML5:离线存储——segmentfault HTML5的离线存储 简介 HTML5提供了很多新的功能以及相应的接口,离线存储就是其中的一个.通过浏览器访问Web App需要联网发送请 ...
- 部署和调优 3.2 dns安装配置-2
配置一个自定义的域,随便定义的,不实际存在. 在配置文件里,增加一个域 vim /etc/named.conf zone "123.com" IN { type master; f ...
- LAMP 2.3 Apache配置防盗链
如果你的站点是一个图片站,有很多非常漂亮的美女图片,那我相信,时间久了会有很多人来你网站借图片,有的人直接下载走了,还有的人直接取走图片的地址,比如你的网站域名是 www.123.com,图片地址为 ...
- DAY10-MYSQL初识
一 数据库管理软件的由来 基于我们之前所学,数据要想永久保存,都是保存于文件中,毫无疑问,一个文件仅仅只能存在于某一台机器上. 如果我们暂且忽略直接基于文件来存取数据的效率问题,并且假设程序所有的组件 ...
- python中not的用法
python中的not具体表示是什么: 在python中not是逻辑判断词,用于布尔型True和False,not True为False,not False为True,以下是几个常用的not的用法: ...
- VC++ 6.0 快捷键
多行注释的快捷键 详细步骤 工具栏上右键-〉Customize-〉“Add-ins and Macro Files”tab页,把SAMPLE前面打上钩-〉“Commands”tab页,Categ ...