2011-05-10 45 views
1

让我们来看看实现代码如下的一些标准规范如何dequeueReusableCellWithIdentifier:处理不同的indexPath?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    static int count = 0; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     NSLog(@"count %d", count++); 
    } 
    // Configure the cell... 
    cell.textLabel.text = [array objectAtIndex:indexPath.row]; 
    return cell; 
} 

如果我们在阵列10个项目,那么它将ALLOC 10个细胞的每个indexPath.row

我的问题是 请问该功能reuseIdentifier :CellIdentifier知道不同的行?

回答

3

如果我们在阵列10个项目,那么它就会 ALLOC 10个细胞的每个 indexPath.row

这是不正确的 - 通常数量分配单元将NumberOfVisibleRows + 2(或相似的价值)。 (大致)UITableView是如何工作的:一旦某个单元格从可见区域滚出,它将从UITableView视图层次结构中移除(将表格的clipToBounds属性设置为NO,您将可以看到!),并将其放入到某种内部缓存。 dequeueReusableCellWithIdentifier:方法检查是否存在可在该缓存中重用的单元并将其返回(并且如果没有单元可用,则返回nil) - 因此,您不需要分配更多的单元,然后实际适合可见区域。

dequeueReusableCellWithIdentifier方法不依赖于当前indexPath,而是依赖于传递给它的单元格字符串标识符。 indexPath仅用于为单元格获取适当的内容。

-1

哦,方法**dequeueReusableCellWithIdentifier**:与indexPath无关。

当使用UITableView时,我们使用[[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault] reuseIdentifier:cellIdentifier]将一个非常亲切的单元格放入缓存(可能是一个NSMutableDictionary)中,并使用键'cellIdentifier'。然后,当我们需要一个单元时,我们首先查询缓存,如果错过了,我们创建一个新缓存。

indexPath是编码员的责任。按区域分隔不同类型的单元格,并在一节中逐行显示单元格。部分和行组成了一个NSIndexPath

相关问题