您的位置:首页 > 产品设计 > UI/UE

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

2013-07-16 13:40 405 查看



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

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

首先创建一个空的xib文件,然后拖拽一个cell放在其上面,记得设置其属性Identifier,假设设置为“mycell”,则我们在代码中请求cell的时候就应该如下写:
        NSString *identifier = @"mycell";  
UITableViewCell *cell = [tView dequeueReusableCellWithIdentifier: identifier];  
if (!cell) {  
        cell = [[[NSBundle mainBundle] loadNibNamed:identifier owner:self options:nil] lastObject];  
   }  
return cell; 

2、cell做为controller的插口变量

声明的时候应该写
@property (nonnonatomic, assign) IBOutlet UITableViewCell *tvCell;   
@synthesize tvCell  

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

cellForRowAtIndexPath函数实现为:
static NSString *MyIdentifier = @"MyIdentifier";  
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];  
if (cell == nil) {  
[[NSBundle mainBundle] loadNibNamed:@"TVCell" owner:self options:nil];  
cell = tvCell;  
self.tvCell = nil;  


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

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

头文件中:
 ApplicationCell *tmpCell;  
// referring to our xib-based UITableViewCell ('IndividualSubviewsBasedApplicationCell')  
UINib *cellNib;  
@property (nonnonatomic, retain) IBOutlet ApplicationCell *tmpCell;  
@property (nonnonatomic, retain) UINib *cellNib; 

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

cellForRowAtIndexPath实现:

 static NSString *CellIdentifier = @"ApplicationCell";  
    ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
    if (cell == nil)  
    {  
 
        [self.cellNib instantiateWithOwner:self options:nil];  
cell = tmpCell;  
self.tmpCell = nil;  
    } 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐