2012-07-17 162 views
2

此加载的UITableViewCell特殊的方式是一种方法,我发明了从笔尖

1)我用我的UITableViewCell类,扩展

//.h 

@interface UITableViewCell (Extended) 

+ (id) cellWithClass:(Class)class; 

+ (id) cellWithClass:(Class)class fromNibNamed:(NSString *)nibName; 

@end 

//.m 

+ (id) cellWithClass:(Class)class 
{ 
    return [UITableViewCell cellWithClass:class fromNibNamed:NSStringFromClass(class)]; 
} 

+ (id) cellWithClass:(Class)class fromNibNamed:(NSString *)nibName { 

    NSArray * nibContents = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:NULL]; 

    NSEnumerator * nibEnumerator = [nibContents objectEnumerator]; 
    NSObject * nibItem = nil; 

    while ((nibItem = [nibEnumerator nextObject]) != nil) { 

     if ([nibItem isKindOfClass:class]) { 
      return nibItem; 
     } 

    } 

    return nil; 
} 

2)创建自定义的UITableViewCell子类加载定制单元,用笔尖相同的名称(CustomCell.xib)其中,i都出口连接

@interface CustomCell : UITableViewCell 

@property (weak, nonatomic) IBOutlet UILabel * labelSmth; 

- (void) setupWithTitle:(NSString *)title; 

@end 

2)在CustomCell.xib使用界面生成器i的拖动一个UITableViewCell并使其类CustomCell(重用标识符CustomCell)(我不设置文件所有者)...比做UI风格,连接插座等..

3)比装载方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString * identifier = @"CustomCell"; 

    CustomCell * cell = [self.tableView dequeueReusableCellWithIdentifier:identifier]; 

    if (cell == nil) { 

     cell = [UITableViewCell cellWithClass:[CustomCell class]]; 

    } 

    [CustomCell setupWithTitle:[self.titles objectAtIndex:[indexPath row]]]; 

    return cell; 
} 

* 这种方法行吗?这工作了很多项目,但IAM不知道的reuseidentfier和大约如果细胞被正确重用的事实... *

IAM还不能确定这个

NSArray * nibContents = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:NULL]; 

因为我通过业主自治在一个类的方法...

也是苹果已经想出了

- (void) registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)reuse; 

这怎么可能适合我的方法呢?

以及如何使用自定义的重用标识符,就像如果我想的方法

+ (id) cellWithClass:(Class)class fromNibNamed:(NSString *)nibName reuseIdentifier:(NSString *)reuseIdentifier; 

回答

3

你并不需要发明新的东西这一点。它已经为你发明了。而你发明的是加载自定义单元的常见反模式。

枚举笔尖内容以获取笔尖中的UITableViewCell不是正确的方法。

您应该在创建UITableViewCell(通常是UIViewController)的笔尖的文件所有者中为您的UITableViewCell定义和输出。

然后你就可以使用这种模式访问单元:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
    static NSString *cellIdentifier = @"MyCustomCell"; //this should also be specified in the properties of the UITableViewCell in the nib file 
    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if(!cell) { 
     [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil]; 
     cell = self.myCustomCellOutlet; 
     self.myCustomCellOutlet = nil; 
    } 

    return cell; 
} 
+0

请问你的样品得到的影响与新的 - (空)registerNib:(UINib *)笔尖forCellReuseIdentifier:(的NSString *)再利用; ? – 2012-07-23 08:45:19

+0

如何使cellIdentifier为NIB?这不会起作用! – applefreak 2014-01-15 15:28:15

+0

你是什么意思? @applefreak – bandejapaisa 2014-01-20 16:21:54