2012-07-17 158 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