iPhone开发中关于Xib文件创建UITableViewCell方法

移动开发 iOS
本文介绍的是iPhone开发中关于Xib文件创建UITableViewCell方法,主要死来介绍使用Xib文件创建UITableViewCell几种不同的方法,来看详细内容。

iPhone开发中关于Xib文件创建UITableViewCell是本文要介绍的内容,主要是来学习如何使用XIB文件创建UITableViewCell的几种方法,来看本文详细内容。

1、cell不做为controller的插口变量

首先创建一个空的xib文件,然后拖拽一个cell放在其上面,记得设置其属性Identifier,假设设置为“mycell”,则我们在代码中请求cell的时候就应该如下写:

  1.         NSString *identifier = @"mycell";  
  2. UITableViewCell *cell = [tView dequeueReusableCellWithIdentifier: identifier];  
  3. if (!cell) {  
  4.         cell = [[[NSBundle mainBundle] loadNibNamed:identifier owner:self options:nil] lastObject];  
  5.    }  
  6. return cell; 

2、cell做为controller的插口变量

声明的时候应该写

  1. @property (nonnonatomic, assign) IBOutlet UITableViewCell *tvCell;   
  2. @synthesize tvCell  

创建nib文件的时候要把file owner选择成当前的controller,然后把IBOut连接起来。

cellForRowAtIndexPath函数实现为:

  1. static NSString *MyIdentifier = @"MyIdentifier";  
  2. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];  
  3. if (cell == nil) {  
  4. [[NSBundle mainBundle] loadNibNamed:@"TVCell" owner:self options:nil];  
  5. cell = tvCell;  
  6. self.tvCell = nil;  

我们可以通过UINib来提高性能

使用UINib类可以大大的提高性能,正常的nib 加载过程包括从硬盘读取nib file,并且实例化对象。但是当我们使用UINib类时,nib 文件只用从硬盘读取一次,读取之后,内容存在内存中。因为其在内存中,创建一序列的对象会花费很少的时间,因为其不再需要访问硬盘。

头文件中:

  1.  ApplicationCell *tmpCell;  
  2. // referring to our xib-based UITableViewCell ('IndividualSubviewsBasedApplicationCell')  
  3. UINib *cellNib;  
  4. @property (nonnonatomic, retain) IBOutlet ApplicationCell *tmpCell;  
  5. @property (nonnonatomic, retain) UINib *cellNib; 

viewDidLoad中:

  1. self.cellNib = [UINib nibWithNibName:@"IndividualSubviewsBasedApplicationCell" bundle:nil]; 

cellForRowAtIndexPath实现:

 

  1.  static NSString *CellIdentifier = @"ApplicationCell";  
  2.     ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  3.     if (cell == nil)  
  4.     {  
  5.  
  6.         [self.cellNib instantiateWithOwner:self options:nil];  
  7. cell = tmpCell;  
  8. self.tmpCell = nil;  
  9.     } 

小结:iPhone开发中关于Xib文件创建UITableViewCell方法的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-07-29 13:27:48

iPhone 开发 Nib

2011-08-08 14:07:49

iPhone开发 字体

2011-08-16 18:56:11

iPhone开发Three20

2011-08-19 10:35:19

iPhone应用Three20

2011-08-09 17:12:30

iPhoneCFRunLoop

2011-08-22 15:15:49

iPhone开发NSMutableAr排序

2011-08-15 13:44:07

iPhone开发UITableView

2011-08-22 14:21:24

iPhone开发UIView Anim

2011-03-16 11:22:16

iconDefaultiPhone

2011-08-12 14:33:06

iPhone缓存文件

2011-07-19 15:33:57

iPhone

2011-08-18 10:39:46

iPhone开发界面

2011-07-06 17:40:43

iPhone SDK

2011-07-20 15:42:18

iPhone 划动条

2011-07-29 14:18:46

iPhone开发 动画

2011-08-09 14:42:07

iPhonePCM播放器

2011-08-12 10:09:23

iPhone开发多线程

2011-08-05 10:13:45

iPhone开发工具 Cocoa Xcode

2011-08-22 11:49:20

iPhone文件系统NSFileManag

2011-08-08 10:23:41

iPhone 流播放 文件
点赞
收藏

51CTO技术栈公众号