【发布时间】:2012-11-02 23:48:18
【问题描述】:
我正在使用重用标识符以编程方式创建单元格。
注意 - 我没有使用故事板来创建单元格
每当cell出队时,cell都是nil,所以需要使用alloc重新创建cell,成本高。
编辑(增加了 1 个问题并更正了代码)
问题
- 为什么这个出队总是返回 nil ?我该如何纠正?
- 只有与情节提要/nib 文件一起使用时,出队才有效吗?
代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell) //Every time cell is nil, dequeue not working
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
【问题讨论】:
-
它返回 nil 的频率是多少?在您的设备屏幕上同时显示多少个单元格?重用意味着只重用那些刚刚从屏幕上滚动出来的单元格。
-
斯里卡是对的。必须在新创建的单元格上设置适当的单元格标识符。
-
它总是返回 nil。在给定时间显示 4 个单元格,当我滚动出队时返回 nil。
标签: objective-c ios uitableview deque