自定义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 ...
随机推荐
- 期望DP
BZOJ 1415 #include <iostream> #include <cstring> #include <algorithm> #include < ...
- H5版俄罗斯方块(2)---游戏的基本框架和实现
前言: 上文中谈到了H5版俄罗斯方块的需求和目标, 这次要实现一个可玩的版本. 但饭要一口一口吃, 很多东西并非一蹴而就. 本文将简单实现一个可玩的俄罗斯方块版本. 下一步会引入AI, 最终采用coc ...
- HDU-2222 Keywords Search(AC自动机--模板题)
题目大意:统计一共出现了多少次模板串. 题目分析:AC自动机的模板题.不过这题有坑,相同的模板串不能只算一次. 代码如下: # include<iostream> # include< ...
- Nginx 下配置SSL证书的方法
1.Nginx 配置 ssl 模块 默认 Nginx 是没有 ssl 模块的,而我的 VPS 默认装的是 Nginx 0.7.63 ,顺带把 Nginx 升级到 0.7.64 并且 配置 ssl 模块 ...
- 5天玩转C#并行和多线程编程
5天玩转C#并行和多线程编程系列文章目录 5天玩转C#并行和多线程编程 —— 第一天 认识Parallel 5天玩转C#并行和多线程编程 —— 第二天 并行集合和PLinq 5天玩转C#并行和多线程编 ...
- 用ABBYY提取文本和表格的方法
在ABBYY FineReader 12 OCR文字识别软件中,有一个插件ABBYY Screenshot Reader,通常情况下与ABBYY FineReader 12一起安装到计算机中,它是一款 ...
- powerdsigner Association Multiplicity
这一篇來告诉一个不容易分辨的关系图式:Association(结合)的各种類型,除了了解它的涵义 外,也让各位可以看图說故事,知道它背后所要表达的意义. Association结合 Associati ...
- jQuery中.bind() .live() .delegate() .on()的区别
bind(type,[data],fn) 为每个匹配元素的特定事件绑定事件处理函数 $("a").bind("click",function(){alert(& ...
- HA(High available)-Keepalived高可用性集群(双机热备)单点实验-菜鸟入门级
HA(High available)-Keepalived高可用性集群 Keepalived 是一个基于VRRP虚拟路由冗余协议来实现的WEB 服务高可用方案,虚拟路由冗余协议 (Virtual ...
- 三线程连续打印ABC
package test5; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Reentr ...