Xib文件创建UITableViewCell

来源:互联网 发布:深红累之渊 知乎 编辑:程序博客网 时间:2024/05/29 01:54

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:@"TVCell" 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 加载过程包括从硬盘读取nibfile,并且实例化对象。但是当我们使用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.     

Lookingaround the App Store, I see most apps customizetheirUITableViews in a uniqueway.

Flixster embeds movie posters and ratings, in addition to theirtitles.

Tweetie integrates tweets, icons, usernames, and the date.

GasBuddy lists service type, amount spent, gallons, and dollarsper gallon in each row.

Constructing thesecustomizedUITableViewCells is possible incode, but leveraging Interface Builder’s drag-and-drop interface isfar more fun.

Thanks to Bill Dudney for talking about one approach to thison hisblog and to StackOverflowfor coveringthis topic.

 

Creating acustom UITableViewCell usingInterface Builder is straight-forward.

  1. In Xcode, create anew UITableViewCell subclassand add thedesired IBOutlets to theheader file.
  2. In Interface Builder, create an “Empty” XIB from the CocoaTouch palette.
  3. Drag a UITableViewCell from the Library into it, configure theclass to be your newcustom UITableViewCell subclass,and give it the appropriate identifier.
  4. Add the desired elements tothe UITableViewCell andconnect the subclass’s outlets to them.
  5. To instantiate the cells usingthe UIViewController approach,set class of the new XIB’s “File’s Owner” to be “UIViewController”,and connectits view outlet to thecustomized UITableViewCell.

The XIB is set up, but there are two approaches to instantiatingthe new customized cell from that XIB. When I was at WWDC a coupleweeks ago, I confirmed with one of the Interface Builder engineersat the IB Lab that both work just fine. (He did repeatedly ask if Iwas usingUITableView:dequeueReusableCellWithIdentifier:,just to make sure.)

 

 

 

UIViewController

One approach is to create atemporary UIViewController eachtime you need a new cell. By setting up the XIB the way we did, thetemporaryUIViewController has the cell asits view attribute. After we grab a pointer to that, we can releasethe view controller.

 

 

view plain
  1. (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2.     UITableViewCell *cell [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"];  
  3.     if (cell == nil)  
  4.         // Create temporary UIViewController to instantiate the custom cell.  
  5.         UIViewController *temporaryController [[UIViewController alloc] initWithNibName:@"BDCustomCell" bundle:nil];  
  6.         // Grab pointer to the custom cell.  
  7.         cell (BDCustomCell *)temporaryController.view;  
  8.         // Release the temporary UIViewController.  
  9.         [temporaryController release];  
  10.      
  11.    
  12.     return cell;  
  13.  
 

 

 

 

 

NSBundle:loadNibNamed:owner:options:

Another approach is to load the NIB file and grab the celldirectly, as it’s the only top-level object in the NIB.

 

 

view plain
  1. (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2.     UITableViewCell *cell [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"];  
  3.     if (cell == nil)  
  4.         // Load the top-level objects from the custom cell XIB.  
  5.         NSArray *topLevelObjects [[NSBundle mainBundle] loadNibNamed:@"BDCustomCell" owner:self options:nil];  
  6.         // Grab pointer to the first object (presumably the custom cell, as that's all the XIB should contain).  
  7.         cell [topLevelObjects objectAtIndex:0];  
  8.      
  9.    
  10.     return cell;  
  11.  
 

 

0 0
原创粉丝点击