自定义UITableViewCell
随着日常的使用,系统提供的cell已经不能满足开发的需要,因为系统提供的是单一的,所以 这就引来了自定义cell的出现,可以根据 自己的需要来布局各个控件所处的位置。不同位置显示不同的控件。
创建一个类,继承于UITableCell.
自定义cell,简单的来说可以分为三步
1.将所有cell要显示的子视图控件声明成属性
@interface MyTableViewCell : UITableViewCell @property (nonatomic, retain)UIImageView *headerImageView; //头像
@property (nonatomic, retain)UILabel *nameLabel; // 姓名
@property (nonatomic, retain)UILabel *genderLabel; // 性别
@property (nonatomic, retain)UILabel *ageLabel; //年龄
2.重写cell的初始化方法,frame给定为0,将控件添加到cell上面进行显示,一定要注意使用self.contentView添加
//CGRectZero表示0
//重写cell的初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self setupSubviews];
}
return self;
}
- (void)setupSubviews{
_headerImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
_headerImageView.backgroundColor = [UIColor greenColor];
//自定义cell内部添加子视图时,不能使用self,应该是使用self.contentView
[self.contentView addSubview:_headerImageView]; _nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_nameLabel.backgroundColor = [UIColor grayColor];
[self.contentView addSubview:_nameLabel]; _genderLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_genderLabel.backgroundColor = [UIColor cyanColor];
[self.contentView addSubview:_genderLabel]; _ageLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_ageLabel.backgroundColor = [UIColor yellowColor];
[self.contentView addSubview:_ageLabel];
}
3.重写layouSubviews方法,给定内部控件的具体位置
//指定内部控件的大小
- (void)layoutSubviews{
[super layoutSubviews];
_headerImageView.frame = CGRectMake(, , , );
_nameLabel.frame = CGRectMake(, , , );
_genderLabel.frame = CGRectMake(, , , );
_ageLabel.frame = CGRectMake(, , , );
}
但如果想方便以后操作的话,可以在自定义cell的时候,使用到数据模型,方便赋值,取值。
1.导入模型,将模型与cell进行绑定,声明模型属性
//在cell内部绑定一个模型属性
@property (nonatomic, retain)Student *stu;
2.重写模型属性的setter方法,内部使用模型为内部控件赋值。
//重写模型的setter方法,完成赋值
- (void)setStu:(Student *)stu{
if (_stu != stu) {
_stu = [stu retain];
//为内部控件进行赋值
_headerImageView.image = [UIImage imageNamed:_stu.picture];
_nameLabel.text = _stu.name;
_genderLabel.text = _stu.gender;
_ageLabel.text = _stu.age;
}
}
如果是在MRC环境下使用的话,一定要注意内存管理哦。
自定义cell的使用方法与系统提供的cell使用方法,没有区别。
//cell显示的内容,数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuseIdentifier = @"reuse";
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]autorelease];
//cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//使用模型属性来赋值
Student *student = _dataArray[indexPath.row];
cell.stu = student;
// cell.nameLabel.text = student.name;
// cell.ageLabel.text = student.age;
// cell.genderLabel.text = student.gender;
return cell;
}
自定义cell,一个重点就是内部各控件该怎么样布局,控件显示的属性有哪些、而怎么样局面,摆放的位置还是要自己设计清楚的。
自定义UITableViewCell的更多相关文章
- 自定义UITableViewCell实现左滑动多菜单功能LeftSwipe
今天愚人节,小伙们,愚人节快乐! 实现一个小功能,滑动菜单,显示隐藏的功能菜单, 先上图: 这里尝试用了下使用三个方式来实现了这个功能: 1.使用自定义UI ...
- 【转】自定义UITableViewCell(registerNib: 与 registerClass: 的区别)
自定义UITableViewCell大致有两类方法: 使用nib 1.xib中指定cell的Class为自定义cell类型(注意不是设置File's Owner的class) 2.调用 tableVi ...
- 如何得到自定义UITableViewCell中的按钮所在的cell的indexPath.row
在自定义UITableViewCell中创建了一个按钮. 想在点击该按钮时知道该按钮所在的cell在TableView中的行数.就是cell的 indexPath.row两种方法都很好.-(IBAct ...
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...
- 新手教程之使用Xib自定义UITableViewCell
新手教程之使用Xib自定义UITableViewCell 前言 首先:什么是UITableView?看图 其次:什么是cell? 然后:为什么要自定cell,UITableView不是自带的有cell ...
- 【转】iOS 通过xib自定义UITableViewCell【原创】
原文网址:http://blog.it985.com/9683.html 在使用tableView的时候,如果cell的布局过于复杂,通过代码搭建的话不够直观.并且要不停的调整位置,字体什么的.这时, ...
- iOS学习之自定义UItableViewCell
在项目开发中,大部分情况下我们都需要自定义UITableViewCell, 今天就重点整理一下目前自己已经学过的自定义Cell的一些注意事项; 分步骤来写吧: 1.将自定义的Cell定义为属性; 2. ...
- 通过xib自定义UITableViewCell
通过xib自定义UITableViewCell 一.新建iOS Application工程,选择Single View Application,不要选中Use Storyboard.假设指定的是pro ...
- 114自定义UITableViewCell(扩展知识:为UITableViewCell添加动画效果)
关键操作: 效果如下: ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UITableViewCo ...
随机推荐
- PTA作业
- Turing Tree_线段树&树状数组
Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems abou ...
- Sprint第二个冲刺(第十一天)
看板: 燃尽图:
- Array数组
array_diff_key() 例子 , , 'purple' , , 'yellow' ); var_dump(array_diff_key($array1, $array2));?> 上例 ...
- JS识记
1.重新按照URL地址加载本页? window.location.reload(); 2.JS中实现命名空间一例? <script type="text/javascript" ...
- Updatepanel 注册javascript 方法
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "test", "alert ...
- How to Allow MySQL Client to Connect to Remote MySql
How to Allow MySQL Client to Connect to Remote MySQ By default, MySQL does not allow remote clients ...
- VS2010环境下C++工程相关问题汇总
1.链接其他库调试时产生告警: warning LNK4099: 未找到 PDB“vc100.pdb” 解决方案:属性 -> C/C++ -> 输出文件 -> 程序数据库文件名 -& ...
- linux shell:mysql bin_log定期清理脚本
需求: 1.自动处理mysql bin日志脚本 2.输出可读log 3.保留1周的日志 4.对所有数据库统一处理. 实现过程描述: 思路:两种方式实现 1.mysql目录通过ls获取bin日志 ...
- PDO多种方式取得查询结果
PDO多种方式取得查询结果 01 December 2009 1:26 Tuesday by Sjolzy PDO最大的特点之一是它的灵活性,本节将介绍如何取得查询结果,包括: 数组(数值或关联数组) ...