我想要关于 dequeueReusableCellWithIdentifier:kCellIdentifier
的一些精确度。如果我理解得很好,下面的 NSLOG 应该只打印一次。但事实并非如此。那么 dequeueReusableCell 有什么意义呢?它只对自定义单元有效吗?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kCellIdentifier = @"UITableViewCellStyleSubtitle3";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
if (cell == nil)
{
NSLog(@"creation of the cell");
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [[self.table objectAtIndex:indexPath.row] objectForKey:kTitleKey];
[cell setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.6]];
return cell;
}
最佳答案
只有当初始化的单元格移出屏幕时,它才会发挥作用。
例如,假设您有一个表格 View ,它在屏幕上显示十个单元格,但总共有一百行。首次加载 View 并填充 TableView 时,将初始化十个单元格(因此有多个 NSLog
statements)。当您开始向下滚动时,从屏幕顶部消失的单元格将被放入重用队列。当需要绘制从底部出现的新单元格时,它们将从重用队列中出队,而不是初始化新实例,从而降低内存使用量。
这也是为什么在 if (cell == nil)
条件之外配置单元格属性很重要的原因。
关于iphone - 如何dequeueReusableCellWithIdentifier : work?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6745114/