代码之家  ›  专栏  ›  技术社区  ›  kpower

从UITableView中出列可重用单元格

  •  2
  • kpower  · 技术社区  · 14 年前

    为什么此代码工作正常:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            cell.textLabel.text = [NSString stringWithFormat:@"cell%i%i", indexPath.section, indexPath.row];
        }    
        return cell;
    }
    

    就我所理解的单元格标识符而言,只有当我移动 cell.textLabel.text = ...

    2 回复  |  直到 14 年前
        1
  •  6
  •   nicktmro    14 年前

    尝试创建比您在屏幕上看到的更多的单元格,一旦它们退出队列,它们将不再具有您期望的文本。。。

    你在屏幕上看到的5行左右基本上是可以的,但一旦你开始滚动,你就会看到一些“有趣的”东西:)

        2
  •  0
  •   RupertP    14 年前

    创建的单元格将被重用。这意味着对象被标记为可重用(从而保存对象的完整创建)。

    所以一旦它滚动出屏幕,一个单元就被标记为可重用。因此,在创建新单元格之前,首先检查是否有任何可重用单元格([tableView dequeueReusableCellWithIdentifier:)。