UITableViewCell重用的第一种情况:
NSString * const cellIdentifier = @"InterestingLabelCell";
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
InterestingLabelCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//当cell为空的时候执行
if(cell == nil){
cell = [[[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil] lastObject];}
}
注意:(如果是使用nib构建UITableViewCell)方法很容易忽略的地方是:xib文件中对应的UITableViewCell中的identifier设置成对应的。例如该编码就是:InterestingLabelCell。不然会出现空数据的情况喔。
UITableViewCell重用的第二种情况:
NSString * const cellIdentifier = @"InterestingLabelCell";
//用UiTableView控件注册一个Nib
- (void)viewDidLoad {
[UITableView registerNib:[UINib nibWithNibName:cellIdentifier bundle:nil] forCellReuseIdentifier:cellIdentifier];}
InterestingLabelCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
}