如何从XIB文件加载自定义封装cell xib定义UITableViewCells

使用xib自定义UIView和UITableViewCell的方法
1. 首先,新建一个类,继承自UIView。
2. 创建一个空的xib文件,文件名最好和前面类名一样。在xib中添加一个View,把加载类改成第一步继承的类。
3. 在xib中添加相应的控件,在第一步创建的类中添加相应的IBOutlet,并且连上相应的控件。
4. 使用如下:
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"xib的名字"
owner:self options:nil];
类名 *View = [nib objectAtIndex:0];
注意:在使用View的时候,如果没有哪个操作具有retain的功能,最好显示地把View
retain一次,如果有addObject,addSubview之类的操作则不用。反正记住一点,这个View不是用alloc、copy等创建的,是个自动释放的对象,要特意保留一下它,要不然在以后用到的时候会出错。
UITableViewCell:
新建一个UIViewController,命名为CellViewController,
修改这个UIViewController的基类,使他从UITableViewCell继承。
打开CellViewController的xib
文件。删除xib中的view,添加一个UITableViewCell到xib窗口中,在xib中修改刚刚添加的UITableViewCell的加载类为CellViewController。可以在xib中加入控件。连上IBOutlet。
<span STYLE="CoLor: # @interface CellViewController : UITableViewCell
{<span STYLE="CoLor: # IBOutlet UILabel *<span STYLE="CoLor: # }<span STYLE="CoLor: # @property(nonatomic, retain)UILabel
*<span STYLE="CoLor: # @end
<span STYLE="CoLor: # @implementation
CellViewController<span STYLE="CoLor: # @synthesize
<span STYLE="CoLor: # @end
使用的时候,在cellForRowAtIndexPath函数中如下:
<span STYLE="CoLor: # - (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ <span STYLE="CoLor: # CellViewController * <span STYLE="CoLor: # static NSString * CellIdentifier = @"CustomAccountStateCellViewControllerIdentifier";
<span STYLE="CoLor: # cell =
(CellViewController
*)[tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
<span STYLE="CoLor: # if(
cell == nil ) <span STYLE="CoLor: # { <span STYLE="CoLor: # NSArray * nib
= [[NSBundle mainBundle]
loadNibNamed:@"CellViewController" owner:self
options:nil] ; <span STYLE="CoLor: #
cell = [nib
objectAtIndex:<span STYLE="CoLor: #]; <span STYLE="CoLor: # }<span STYLE="CoLor: #
<span STYLE="CoLor: # cell.lb.text = @"ABC";<span STYLE="CoLor: # return <span STYLE="CoLor: # }
当然要添加自定义Cell的头文件到这里来。
运行程序。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。下次自动登录
现在的位置:
& 综合 & 正文
[IOS]用自定义的cell来创建UITableView
1.创建自定义的CellView.xib,操作New File-&User Interface-&View-&命名cellView
2.往上面拖放一个UITableViewCell,然后向其中拖放添加UILabel,UITextField,UIButton,如下图:
3.创建一个类Cell,操作New File-&Cocoa Touch-&Objective-C class-&Class:Cell,Subclass of:UITableViewCell
4.将cellView.xib中的Cell的class设置为Cell类,然后可以将label关联到Cell.h中,也可以设置Label的Tag属性为1,后面有两种方法调用xib文件,一种是通过tag,其次就是通过cell。注意:xib文件中的File's Owner只能关联到viewController,这里自定义的cell.xib没有可以关联的contrller文件,所以不需要写,只需要把xib中的cell控件关联到cell类就可以了
5.ViewController.h:
#import &UIKit/UIKit.h&
@interface ViewController : UIViewController&UITableViewDataSource,UITableViewDataSource&
@property (retain, nonatomic) IBOutlet UITableView *tableV
@property(retain,nonatomic)NSArray *
6.ViewController.m:
#import "ViewController.h"
#import "Cell.h"
static NSString *strIdentifier = @"strIdentifier";
@interface ViewController ()
@implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
- (void)viewDidLoad
[super viewDidLoad];
self.arr = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
//获得包含tableViewcell的nib文件
UINib *nib = [UINib nibWithNibName:@"cellView" bundle:nil];
//用这个nib文件注册成tableView中表示为strIdentifier的cell
[self.tableView registerNib:nib forCellReuseIdentifier:strIdentifier];
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [self.arr count];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//询问是否有未使用的,表示为strIdentifier的cell,如果没有,并且tableview注册过,系统会建一个新的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIdentifier];
//用这个自定义的cell可以用绑定的方式修改内容,要将自定义的cell.xib的class修改成Cell类
((Cell *)cell).label.text = [self.arr objectAtIndex:[indexPath row]];
//也可以用tag的方式查找到需要修改的view
//((UILabel *)[cell viewWithTag:1]).text = [self.arr objectAtIndex:[indexPath row]];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
- (void)dealloc {
[_tableView release];
[self.arr release];
[super dealloc];
运行结果图:
&&&&推荐文章:
【上篇】【下篇】2438人阅读
iOS(280)
可以通过继承UITableViewCell重新自定义cell,可以像下面一样通过代码来自定义cell,但是手写代码总是很浪费时间,
@interface&CustomTableViewCell:UITableViewCell
@property&(nonatomic,&strong)&UIImageView&*headImageV
@property&(nonatomic,&strong)&UILabel&*nameL
@property&(nonatomic,&strong)&UILabel&*textL
@implementation&CustomTableViewCell
-&(id)initWithStyle:(UITableViewCellStyle)style&reuseIdentifier:(NSString&*)reuseIdentifier
&&&&self&=&[super&initWithStyle:style&reuseIdentifier:reuseIdentifier];
&&&&if&(self)&{
&&&&&&&&_headImageView&=&[[UIImageView&alloc]&init];
&&&&&&&&[self.contentView&addSubView:_headImageView];
&&&&&&&&_nameLabel&=&[[UILabel&alloc]&init];
&&&&&&&&[self.contentView&addSubView:_nameLabel];
&&&&&&&&_textLabel&=&[[UILable&alloc]&init];
&&&&&&&&[self.contentView&addSubView:_textLabel];
&&&&return&
-&(void)layoutSubviews
&&&&self.headImageView.frame&=&CGRectMake(.....);
&&&&self.nameLabel.frame&=&CGRectMake(.....);
&&&&self.textLabel.frame&=&CGRectMake(....);
&&&&[super&layoutSubviews];
上面CustomTableViewCell的.h/.m文件中,我用了大段的代码来给自定义的cell布局,真的挺麻烦。然后在ViewController中使用的时候是这样的,
-&(UITableViewCell&*)tableView:(UITableView&*)tableView&cellForRowAtIndexPath:(NSIndexPath&*)indexPath
&&&&static&NSString&*simpleIdentify&=&@&SimpleIdentify&;
&&&&CustomTableViewCell&*cell&=&[tableView&dequeueReusableCellWithIdentifier:simpleIdentify];
&&&&if(cell&==&nil)
&&&&&&&&cell&=&[[CustomTableViewCell&alloc]&initWithStyle:UITableViewCellStyleDefault&reuseIdentifier:simpleIdentify];
&&&&Person&*person&=&[_persons&objectAtIndex:indexPath.row];
&&&&cell.headImage.image&=&[UIImage&imageNamed:person.headStr];
&&&&cell.nameLabel.text&=&person.
&&&&cell.textLabel.text&=&person.speechT
&&&&return&
上面就是使用代码自定义cell所要做的工作,我们当cell显示没有达到预期的时候我们还要回头改变其中UI控件的坐标,或者更改控件属性,当然这只是要多花点时间,最终还是可以实现的。
但是。。。我们还是要试着提高自己的开发效率,使用越来越成熟的xib技术,可以很大地提高开发效率,当然也是要勤加练习,熟能生巧。
开始阅读下面的内容之前,我假设你已经看过我-UITableView系列1,那篇博客是本篇博客的基础;如果你没有看过,那就看看吧,然后接着看本篇博客。
所以下面开始学习吧!Come on!
按照下面的步骤开始操作,
(1)新建一个空的(Empty)xib文件,File-&New,在面板中选择User Interface-&Empty,如下图,
将文件命名为CustomTableViewCell,表示自定义的cell意思。
(2)拖动一个TableViewCell到空的(Empty)xib文件中,如下图,
(3)修改CustomTableViewCell的高度为90,通过属性面板来设置,如下图,
(4)拖一个UIIMageView到CustomTableViewCell的xib文件,设置该UIImageView控件的tag&#20540;为10;再拖两个UILabel到xib文件,设置tag分别为1和2,其布局方式如下图,
上面的几个步骤就进行了CustomTableViewCell的自定义,下面的步骤就是使用这个通过xib文件进行自定义的cell。
(5)选中CustomTableViewCell,点击左上角的File's Owner,如下图,
选中这个xib文件中的File's Owner是为了设置CustomTableViewCell的文件所有者,否则无法与.h/.m文件连线,接着看下一步。
(6)在右侧的面板,选择“Show the Identify Inpector”选项,如下图
这时候我们发现CustomTableViewCell的File's Owner为NSObject,因为我要在我的RootViewController中使用,所以我将&NSObject&替换为&RootViewController&,表明这个cell的所有者是RootViewController。(PS:你需要将此处的NSObject改为你使用该CustomTableViewCell的ViewController文件名。)
(7)在使用该CustomTableViewCell的ViewController的头文件中写下如下代码,
@interface&RootViewController:UIViewController
@property&(nonatomic,&strong)&IBOutlet&UITableViewCell&*customC
有人会奇怪这段话是什么意思,接着下面的步骤,你就会明白。
(8)在CustomTableViewCell.xib文件中,拖动File's Owner指向TableViewCell,如下图,
(9)在弹出的界面中选择customCell选项,如下图,
这就是为什么第(7)步骤要在RootViewController.h文件中声明一个IBOutlet关键字修饰的customCell变量的原因了。
(10)新建一个数据模型文件Person,继承自NSObject,其代码如下,
@interface&Person&:&NSObject
@property&(nonatomic,&strong)&NSString&*
@property&(nonatomic,&strong)&NSString&*headS
@property&(nonatomic,&strong)&NSString&*speechT
#import&&Person.h&
@implementation&Person
之所以叫它模型文件,是因为该文件中的属性与CustomTableViewCell上面的控件所需的内容一致,以MVC的视角来看Person就是M(Model),CustomTableViewCell就是V(View),而RootViewController就是C(Controller)。
(11)在RootViewController中初始化TableView数据源_persons,代码如下,
@interface&RootViewController&()&UITableViewDelegate,UITableViewDataSource&
&&&&NSArray&*_
@implementation&RootViewController
-&(void)viewDidLoad
&&&&[super&viewDidLoad];
&&&&Person&*p0&=&[[Person&alloc]&init];
&&&&p0.name&=&@&路飞&;
&&&&p0.headStr&=&@&person0&;
&&&&p0.speechText&=&@&我要当海&#36156;王!&;
&&&&Person&*p1&=&[[Person&alloc]&init];
&&&&p1.name&=&@&卓洛&;
&&&&p1.headStr&=&@&person1&;
&&&&p1.speechText&=&@&受尽磨难而不折,此乃修罗之道!&;
&&&&Person&*p2&=&[[Person&alloc]&init];
&&&&p2.name&=&@&罗宾&;
&&&&p2.headStr&=&@&person2&;
&&&&p2.speechText&=&@&我要活下去,把我带向大海吧!&;
&&&&Person&*p3&=&[[Person&alloc]&init];
&&&&p3.name&=&@&娜美&;
&&&&p3.headStr&=&@&person3&;
&&&&p3.speechText&=&@&帮帮我,路飞!&;
&&&&_persons&=&[NSArray&arrayWithObjects:p0,p1,p2,p3,&nil];
因为我是海&#36156;迷,所以找了路飞、卓洛、娜美、罗宾的图片当做头像,你可以到网上down几张,改名为person0.png、person1.png、person2.png、person3.png。
(12)在-tableView:cellForRowAtIndexPath:中使用CustomTableViewCell.xib创建cell,代码如下,
#pragma&mark&-&UITableView&methods
-&(NSInteger)tableView:(UITableView&*)tableView&numberOfRowsInSection:(NSInteger)section
&&&&return&[_persons&count];
-&(UITableViewCell&*)tableView:(UITableView&*)tableView&cellForRowAtIndexPath:(NSIndexPath&*)indexPath
&&&&static&NSString&*simpleIdentify&=&@&SimpleIdentify&;
&&&&UITableViewCell&*cell&=&[tableView&dequeueReusableCellWithIdentifier:simpleIdentify];
&&&&NSArray&*nib&=&[[NSBundle&mainBundle]&loadNibNamed:@&CustomTableViewCell&&owner:self&options:nil];
&&&&if&([nib&count]&0)
&&&&&&&&self.customCell&=&[nib&objectAtIndex:0];
&&&&&&&&cell&=&self.customC
&&&&Person&*person&=&[_persons&objectAtIndex:indexPath.row];
&&&&UIImageView&*headImageView&=&(UIImageView&*)[cell.contentView&viewWithTag:0];
&&&&UILabel&*nameLabel&=&(UILabel&*)[cell.contentView&viewWithTag:1];
&&&&UILabel&*textLabel&=&(UILabel&*)[cell.contentView&viewWithTag:2];
&&&&headImageView.image&=&[UIImage&imageNamed:person.headStr];
&&&&nameLabel.text&=&person.
&&&&textLabel.text&=&person.speechT
&&&&return&
最终的效果图如下,
总结:上面的步骤看起来复杂,但是当你习惯xib来编程的时候,使用鼠标拖拉空间,微调界面,那种感觉就像自己不是纯粹的程序员,还是一个设计师。
我把源代码放到了上面,有兴趣的朋友可以去下载看看。或者在Mac终端,输入git
clone&/pythonhater/TableViewSamples.git,即可获得源代码。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1966706次
积分:18535
积分:18535
排名:第1476名
原创:236篇
转载:383篇
评论:219条
(4)(10)(4)(2)(4)(10)(6)(9)(6)(7)(1)(4)(4)(8)(13)(20)(13)(13)(13)(13)(12)(18)(31)(11)(14)(24)(13)(4)(19)(13)(12)(7)(10)(3)(6)(17)(23)(33)(18)(17)(29)(18)(1)(8)(2)(4)(8)(6)(12)(4)(10)(5)(3)(2)(1)(2)(3)(5)(5)(1)(1)(2)(1)(2)【原】用xib自定义UITableViewCell的注意事项——重用问题 - 编程小翁 - 推酷
【原】用xib自定义UITableViewCell的注意事项——重用问题 - 编程小翁
问题的提出:
有时候我们经常需要自定义tableView的cell,当cell里面的布局较为复杂时往往舍弃纯代码的方式而改用xib的方式进行自定义。当我们用纯代码的方式布局cell时,往往会在cell的initWithStyle: reuseIdentifier: 方法里面用纯代码进行布局,然后在外部VC的cellForRowAtIndexPath方法里面我们会这么写,假定自定义的cell为Cell,继承自UITableViewCell:
static NSString *CellIdentifier = @&Cell&;
Cell *cell = (Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.titleLabel.text = [self.dataList objectAtIndex:indexPath.row];
上述这么写完全没有问题,因为教科书跟各种教程都是这么写的,上述代码对cell进行了重用。但如果我们现在想用xib来布局呢?看到过不少代码是这么写的:
static NSString *CellIdentifier = @&Cell&;
Cell *cell = (Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([Cell class])
owner:self
options:nil] objectAtIndex:0];
//cell = [[[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.titleLabel.text = [self.dataList objectAtIndex:indexPath.row];return
唯一区别的地方在于红色的位置:cell的初始化不再通过initWithStyle函数,因为我们现在用xib布局,所有的控件信息都在xib里面,因此我们根据Cell所对应的xib名称来加载xib以创建cell。但是你注意到没,第二种方法的红色部分并没有包含任何重用信息,也就是说,每次拖动tableview,都会一直创建不同的cell,当要显示的cell很多时内存问题就显露出来了。
为了解决这个问题,我们换用另一种更好的方式。
直接放优化后的代码,UINib类可以先不看,不影响代码的理解:
static NSString *CellIdentifier = @&Cell&;
BOOL nibsRegistered = NO;
if (!nibsRegistered) {
UINib *nib = [UINib nibWithNibName:NSStringFromClass([Cell class]) bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:CellIdentifier];
nibsRegistered = YES;
Cell *cell = (Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.titleLabel.text = [self.dataList objectAtIndex:indexPath.row];
我们可以看到,红色部分很好地满足了我们的需求:既从nib加载,又能对cell进行重用。下面对代码进行解释:
1.UINib是一个IOS4.0才出现的类,与MAC上的NSNib类作用相似,
就是加速频繁使用的NIB文件的加载。在第一次从硬盘加载NIB时,它在内存中缓存NIB文件对象。之后加载NIB文件时就会从内存拷贝而避免了较慢的硬盘访问。Apple宣称可以在
加载NIB文件时提供
2倍的速度提升
使用UINib的最明显的地方就是在需要在每次创建新Cell时从NIB文件中加载Cell的UITableViewControllers中。UINib的优势就是在不用大量修改代码的情况获得性能改进。其实简单地说,就是利用缓存机制避免了频繁从硬盘中加载XIB文件,这在大数据量的时候显得尤为有用。
2.除了上述代码,还需要在xib文件中做如下设置:在Cell.xib的Inspector窗口中将Identifier进行设置,这里的Identifier要与cellForRowAtIndexPath中一致。
以上就是从xib加载自定义UITableViewCell的注意事项以及解决方案,很多人采用了第二种方法,看起来虽然没问题但是一旦内存吃紧的时候问题就暴露出来了!
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致

我要回帖

更多关于 自定义封装cell xib 的文章

 

随机推荐