static NSString * Identifier = @"pastRecordsCell";
BOOL nibsRegistered = NO;
if (!nibsRegistered) {
UINib * nib = [UINib nibWithNibName:NSStringFromClass([ZSPastRecordsCell class]) bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:Identifier];
nibsRegistered = YES;
}
ZSPastRecordsCell * pastRecordsCell = (ZSPastRecordsCell *)[tableView dequeueReusableCellWithIdentifier:Identifier];
pastRecordsCell.selectionStyle = UITableViewCellSelectionStyleNone;
[pastRecordsCell configPastRecordsCell:self.dataArray[indexPath.row]];
return pastRecordsCell;
除此之外,还需在XIB文件中设置与代码中一样的Identifier。
注意:UINib是iOS4以后出来的类,与MAC上的NSNib类相似。就是对频繁使用的NIB文件的加载。当第一次从硬盘加载NIB是,它在内容中缓存NIB文件对象,之后加载的XIB文件就会从内存中拷贝出来,从而避免了较慢的硬盘访问。使用UINib最明显的地方就是在需要每次创建新Cell时从NIB文件中加载Cell的UITableViewController中。UINib的优势就是在不用大量修改代码的情况下获得性能的改进。apple曾宣称可以在加载UINib文件时提供2倍速度的提升。其原理简单讲就是,利用缓存机制避免频繁的从硬盘中加载XIB文件,这在大数据量的时候尤为突出。
另一种XIB代码重用的示例:
static NSString * Identifier = @"pastRecordsCell";
ZSPastRecordsCell * cell ;
cell = (ZSPastRecordsCell *)[tableView dequeueReusableCellWithIdentifier:Identifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ZSPastRecordsCell class]) owner:self options:nil] lastObject];
}
return cell;
这种加载方式在内存充足的情况下看起来是没问题,一旦内存吃紧的时候问题就暴露出来了!