如何测试tableview的重用机制

2025-03-04 16:23:45
推荐回答(1个)
回答1:

重用机制 简单的说 意思 一行一行 的cell 都是在复用的, 滑动 tableview 的时候,刚离开视图的 cell 会被放到复用池 中,等下一个 cell需要 显示时,会先看复用池中有没有 cell 如果有的时候 ,就从复用池中拿出来cell ,没有的话就重新创建cell

大概就是这几句代码
static NSString *cellName = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName] autorelease];
}

下面是使用。。。people 是另外就个类里的对象,就是一个 数组 里面 取东西, 很好理解的。。。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellName = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName] autorelease];
}
cell.textLabel.text = ((People *)[[_dataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]).peopleName;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",((People *)[[_dataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]).peopleAge];
return cell;
}