此加載的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;
請問你的樣品得到的影響與新的 - (空)registerNib:(UINib *)筆尖forCellReuseIdentifier:(的NSString *)再利用; ? – 2012-07-23 08:45:19
如何使cellIdentifier爲NIB?這不會起作用! – applefreak 2014-01-15 15:28:15
你是什麼意思? @applefreak – bandejapaisa 2014-01-20 16:21:54