30.自定义UITableViewCell第一章
2015-11-24 21:24
375 查看
自定义cell(只要是自定义控件 都这个步骤)
1.创建TableViewCell子类
2.重写初始化方法
3.把要添加的控件 添加cell的显示内容区域 contentView上面
4.把系统的cell 替换成 自定义cell 完成
步骤1.创建TableViewCell子类
@interface MyTableViewCell : UITableViewCell
步骤2.重写初始化方法
1. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self addSubview];
}
return self;
}
步骤3:把要添加的控件 添加cell的显示内容区域 contentView上面
- (void)addSubview
{
self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(kMargin,kMargin,kImageWidth,kImageHeight)];
self.imageV.backgroundColor = [UIColor yellowColor];
[self.contentView addSubview:self.imageV];
[self.imageV release];
self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.imageV.right + kMargin, self.imageV.top, kScreenWidth - 3 * kMargin - self.imageV.right, self.imageV.height/3 - kLabelMargin)];
self.nameLabel.backgroundColor = [UIColor redColor];
[self.contentView addSubview:self.nameLabel];
[self.nameLabel release];
self.phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.nameLabel.left, self.nameLabel.bottom + kLabelMargin, self.nameLabel.width, self.nameLabel.height)];
self.phoneLabel.backgroundColor = [UIColor redColor];
[self.contentView addSubview:self.phoneLabel];
[self.phoneLabel release];
self.ganderLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.phoneLabel.left, self.phoneLabel.bottom + kLabelMargin, self.phoneLabel.width, self.phoneLabel.height)];
self.ganderLabel.backgroundColor = [UIColor redColor];
[self.contentView addSubview:self.ganderLabel];
[self.ganderLabel release];
}
为了别人方便重写model的setter方法 使赋值model的同时 也对控件进行赋值
- (void)setModel:(CellModel *)model
{
if (_model != model) {
[_model release];
_model = [model retain];
}
self.nameLabel.text = model.name;
self.phoneLabel.text = model.phoneNumber;
self.ganderLabel.text = model.gender;
self.imageV.image = [UIImage imageNamed:@”nvshen”];
}
===========================================复合cell
数据加载
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
根据model值进行判断 显示不同的cell
CellModel *model = self.dataArray[indexPath.row];
if ([model.gender isEqualToString:@”男”]) {
创建男的
static NSString *identifier = @”Man=Cell”;
ManTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[ManTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease];
}
再给model赋值的同时 咱们希望 也给cell上的控件完成赋值
cell.model = model;
return cell;
}else{
女的
static NSString *identifier = @”GirlCell”;
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[MyTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease];
}
再给model赋值的同时 咱们希望 也给cell上的控件完成赋值
cell.model = model;
return cell;
}
相关文章推荐
- 【IOS】UILabel 属性及方法
- 29.UITableView表视图第四章(添加高级)
- vue+webpack在“双十一”导购产品的技术实践
- UITableViewCell的选中时的颜色设置
- 【NGUI】通过射线检测被遮挡的物体
- 使用JAXP进行DOM解析( DocumentBuilderFactory、DocumentBuilder、Document)
- 28.UITableView表视图第三章(移动和编辑删除和添加)
- UI学习 第十章 KVC    KVO    通知
- UI学习 第九章 委托(代理)设计模式      单例设计模式
- UI学习 第八章 导航控制器
- UI学习 第七章 UITableView每行的cell不同的设置
- [TwistedFate]UITableViewCell自定义-01
- [APUE]再读之 unix 基础知识
- setMouseTracking(true)无法跟踪鼠标事件
- HDU 2253 Longest Common Subsequence Again
- UI学习 第六章 UITableView
- UI学习 第五章 UIScrollView     UIPageControl
- UI学习 第四章 UIImage    UIImageView    手势
- easyUI..关于datagrid实现追加一行做统计功能
- UIImageView的图片轮播属性