tableview使用dequeuereusablecellwithidentifier

时间:2013-09-12 06:36:30

标签: ios ios6 uitableview

我是iPhone的新手请告诉我在tableview中有人使用dequeuereusablecellwithidentifier。 还告诉我不使用dequeuereusablecellwithidentifier如何在tableview中创建单元格?

1 个答案:

答案 0 :(得分:8)

使用 dequeueReusableCellWithIithntifier 的最佳部分是使用它可以重复使用您的单元格。

想象一下,如果你的表有1000个条目。现在,如果为每个条目创建一个表格单元格,则创建1000个条目,1000个tableview单元格和1000个tableview单元格的内存分配。

如果条目超过1000,应用程序将放慢速度或崩溃。

当我们使用 dequeueReusableCellWithIdentifier 时,tableView会根据您的表格高度和单元格高度精确创建单元格数量。 假设,如果它在tabelView中显示4个单元格,你可以通过滚动查看,那么在任何给定的时间点只会分配4个单元格的内存。

现在,当您滚动tableView时,它将重新使用相同的单元格,但会根据您的数据源更改单元格内容(数据)。

希望这可以解除你的怀疑。

在不使用 dequeueReusableCellWithIithntifier 的情况下添加单元格

代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.textLabel.text = @"Test";
    return cell;
}