dequeueReusableCellWithIdentifier如何:处理不同的indexPath? [英] How dequeueReusableCellWithIdentifier: treat different indexPath?
问题描述
让我们看一下tableview的一些标准代码
Let us look some standard codes of tableview
- (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个项目,那么它将为每个项目分配10个单元格indexPath.row
If we have 10 items in array, then it will alloc 10 cells for each indexPath.row
我的问题是
函数reuseIdentifier:CellIdentifier如何知道不同的行?
推荐答案
如果我们在数组中有10个项目,那么
将为每个
indexPath分配10个单元格.row
If we have 10 items in array, then it will alloc 10 cells for each indexPath.row
这不是真的 - 通常分配的单元格数将是NumberOfVisibleRows + 2(或类似的值)。
That's not true - normally number of allocated cells will be NumberOfVisibleRows+2 (or similar value).
这是(大致)UITableView的工作方式:一旦某个单元格滚出可见区域,它就会从UITableView视图层次结构中删除(将表格的clipToBounds属性设置为NO,而你是'我能够看到!)并被置于某种内部缓存中。 dequeueReusableCellWithIdentifier:
方法检查是否有可在该缓存中重用的单元并返回它(如果没有单元可用则返回nil) - 因此您不需要更多地分配单元那么实际上适合可见区域。
This is (roughly) how UITableView works: Once certain cell is scrolled out of visible area it is removed from UITableView view hierarchy (set table's clipToBounds property to NO and you'll be able to see that!) and is put to a some kind of internal cache. dequeueReusableCellWithIdentifier:
method checks if there's cell available for reuse in that cache and returns it (and returns nil if no cell is available) - so you don't need to allocate cells more then actually fit visible area.
dequeueReusableCellWithIdentifier
方法不依赖于当前的indexPath,而是依赖于你传递的单元字符串标识符它。 indexPath仅供您用于获取单元格的适当内容。
dequeueReusableCellWithIdentifier
method does not depend on current indexPath, rather on cell string identifier you pass to it. And indexPath is used only by you to get appropriate contents to a cell.
这篇关于dequeueReusableCellWithIdentifier如何:处理不同的indexPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!