Xib文件创建UITableViewCell

来源:互联网 发布:昆仑虚 神兵 升阶数据 编辑:程序博客网 时间:2024/05/19 23:27

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 加载过程包括从硬盘读取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.     

Looking around the App Store, I see most apps customize theirUITableViews in a unique way.

Flixster embeds movie posters and ratings, in addition to their titles.

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

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

Constructing these customizedUITableViewCells is possible in code, but leveraging Interface Builder’s drag-and-drop interface is far more fun.

Thanks to Bill Dudney for talking about one approach to this on his blog and to StackOverflow for covering this topic.

 

Creating a custom UITableViewCell using Interface Builder is straight-forward.

  1. In Xcode, create a new UITableViewCell subclass and add the desired IBOutlets to the header file.
  2. In Interface Builder, create an “Empty” XIB from the Cocoa Touch palette.
  3. Drag a UITableViewCell from the Library into it, configure the class to be your new custom UITableViewCell subclass, and give it the appropriate identifier.
  4. Add the desired elements to the UITableViewCell and connect the subclass’s outlets to them.
  5. To instantiate the cells using the UIViewController approach, set class of the new XIB’s “File’s Owner” to be “UIViewController”, and connect its view outlet to the customized UITableViewCell.

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

 

 

 

UIViewController

One approach is to create a temporary UIViewController each time you need a new cell. By setting up the XIB the way we did, the temporaryUIViewController has the cell as its view attribute. After we grab a pointer to that, we can release the 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 cell directly, 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.  

转载:http://blog.sina.com.cn/s/blog_61f4999d0100x7w5.html