我已经在我的项目中实现了 CellForRowAtIndexPath 方法。
看起来像这样:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"StackTableViewCell";
Target *target = [self.fetchedResultController objectAtIndexPath:indexPath];
StackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"StackTableViewCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
cell.cellLabel.text = target.body;
cell.cellLabel.font = [UIFont fontWithName:@"Candara-Bold" size:20];[UIFont fontWithName:@"Candara-Bold" size:20];
// Configure the cell...
return cell;
}
但是有件事让我很困扰,那就是我真的不明白这部分发生了什么(我从一些教程中得到的):
if (!cell)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"StackTableViewCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
有人可以帮我理解这个吗?
谢谢!
最佳答案
如果 TableView 没有可重复使用的单元格,请加载 StackTableViewCell.xib 中存在的对象。假设您找到的第一个定义在顶层的单元可以实例化为您想要的单元格类型。
关于ios - nib 项目中的 CellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27534768/