ios使用xib文件创build自定义UITableViewCell的步骤

我需要创build我自己的UITableViewCell使用XIB文件,绘制graphics界面…什么是正确的步骤来创build我的新类,并使用到我的UITableView?

提前致谢

在iOS5中,您将需要使用新的:

registerNib:forCellReuseIdentifier

基本上做同样的事情…

就个人而言,我认为这两个build议的教程在重新使用reuseIdentifier有一个很大的缺陷。 如果您忘记在界面构build器中分配它或拼写错误,则每次调用cellForRowAtIndexPath时都会加载nib。

杰夫LaMarche写这个和如何解决这个博客文章 。 除了reuseIdentifier他使用与在加载自定义表格 – 查看单元格从Nib文件的Apple文档中相同的方法。

在阅读所有这些文章后,我想出了以下代码:

编辑:如果你的目标iOS 5.0和更高,你会想坚持杜安领域的答案

 @interface CustomCellWithXib : UITableViewCell + (NSString *)reuseIdentifier; - (id)initWithOwner:(id)owner; @end @implementation CustomCellWithXib + (UINib*)nib { // singleton implementation to get a UINib object static dispatch_once_t pred = 0; __strong static UINib* _sharedNibObject = nil; dispatch_once(&pred, ^{ _sharedNibObject = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil]; }); return _sharedNibObject; } - (NSString *)reuseIdentifier { return [[self class] reuseIdentifier]; } + (NSString *)reuseIdentifier { // return any identifier you like, in this case the class name return NSStringFromClass([self class]); } - (id)initWithOwner:(id)owner { return [[[[self class] nib] instantiateWithOwner:owner options:nil] objectAtIndex:0]; } @end 

UINib(在iOS 4.0和更高版本中可用)在这里用作单例,因为尽pipe使用了reuseIdentifier ,但单元格仍然会被重新初始化大约10次左右。 现在cellForRowAtIndexPath看起来像这样:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CustomCellWithXib *cell = [tableView dequeueReusableCellWithIdentifier:[CustomCellWithXib reuseIdentifier]]; if (cell == nil) { cell = [[CustomCellWithXib alloc] initWithOwner:self]; } // do additional cell configuration return cell; } 

展示了如何使用Xcode 4.2做到这一点的video教程 。 作者也写了一篇博文 。

本教程将带您了解整个现代iOS 5解决scheme,从创build单元的xib和类文件的过程到完成:

http://mrmaksimize.com/ios/Custom-UITableViewCell-With-NIB/

`你可以在.xib文件的帮助下在表格视图中创build自定义单元格。 首先在视图控制器中设置表视图,用它的类创build一个新的xib文件,并在表视图中使用它。

 - (IBAction)moveToSubCategory:(id)sender; @property (strong, nonatomic) IBOutlet UILabel *foodCategoryLabel; @property (strong, nonatomic) IBOutlet UIImageView *cellBg; -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [foodCatArray count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"ExampleCell"; ExampleCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ExampleCell" owner:self options:nil]; cell = [nib objectAtIndex:0]; } [cell setTag:indexPath.row]; cell.cellBg.image=[UIImage imageNamed:[photoArray objectAtIndex:indexPath.row]]; cell.foodCategoryLabel.text=[foodCatArray objectAtIndex:indexPath.row]; return cell; } 

您可以使用从UITableViewCellinheritance的XIB创buildCustomCell类。 我们将按照以下方式在tableview类.m文件中添加类别。 我认为这是应用于自定义单元格创build的最简单的方法。


     @interface UITableViewCell(NIB)
     @property(非primefaces,读写,复制)NSString * reuseIdentifier;
     @结束
     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     {
        返回30;
     }

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {
    静态NSString *标识符= @“单元格”;
         CustomCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        如果(细胞==无)
         {
              NSLog(@“New Cell”);
             NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@“CustomCell”owner:self options:nil];
             cell = [nib objectAtIndex:0];
             cell.reuseIdentifier =标识符;

         }其他{
             NSLog(@“Reuse Cell”);
         }
         cell.lbltitle.text = [NSString stringWithFormat:@“Level%d”,indexPath.row];
         id num = [_ arrslidervalues objectAtIndex:indexPath.row];
         cell.slider.value = [num floatValue];
        返回小区;
     }
     @结束