0

我对重用 UITableViewCell 有点怀疑。

当我们创建 UITableViewCell 时,它看起来像下面这样。

- (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];
          [self configureCell:cell forIndexPath:indexPath];
      }
}

- (void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {/**Cell Config Code Goes Here**/}
}

所以在我的例子中,UITableView 中的每个单元格都是不同的。如果 UITableView 重用单元格,则单元格内容完全不同。

将 CellIdentifier 传递为 nil 是否是一种好习惯,以便每次创建新单元时,而不是在所有单元都不同的条件下?

或者我应该只是移动 [self configureCell:cell forIndexPath:indexPath]; 出来自己处理?

4

3 回答 3

0

恐怕您必须将配置单元格代码移出 if 条件以使每个单元格都有自己的内容。

    - (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];  
          }  
          [self configureCell:cell forIndexPath:indexPath];  
    }

虽然我们说重用 UITableViewCells,但我们的意思是我们不必每次都在单元格内创建 UIView 层次结构。但是您需要为不同的单元格配置内容。像 cell.titleLabel.text = xxxx。

同时,您可以为不同类型的单元格使用多个重用标识符。或者,如果您只有一个这样的单元格,您可以将一个单元格定义为属性实例,这样您就不必每次都创建它。

于 2014-07-21T05:55:30.510 回答
0

如果您多次使用单元格内容(相同的子视图),则单元格可重用性是有意义的。就像您的 tableViewcell 中有两个标签用于 tableView 中的所有行。如果您有少量不同的单元格。就像如果您在 tableView 中多次使用三种类型的单元格,您可以使用具有 3 个不同单元格标识符的单元格可重用性。

但是如果你有所有不同的单元格,那么如果你跳过单元格的可重用性就可以了。

于 2014-07-21T05:58:32.933 回答
0

使用 tableView 的可重用性的正确方法如下所示。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"FollowerCell";

UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil)
{
    cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[self configureCell:cell forIndexPath:indexPath];

return cell;
}


- (void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {/**Cell Config Code Goes Here**/}
}

可重用性的基本思想是每次不应创建相似类型的单元格,而应仅通过更新其内容来重用它们。

幕后发生的事情是创建了一个队列,其中添加了这些相似的单元格。现在假设有 200 行不同的数据,但只有 10 行是可见的。在队列中只会出现大约 14 个单元格。现在,当您向上或向下滚动表格视图时,此条件 UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

检查队列是否包含任何单元格,如果是,则从队列中取出一个单元格。此外,之前在消失时可见的单元格也被添加到队列中。这样,每次创建新单元时,都会使用已创建的单元。

现在,如果您强制使单元格 = nil,则每次都会创建新单元格并将其添加到队列中。现在,如果有 200 个数据,则队列将包含 200 个单元,从而导致内存大小增加。

希望它能帮助你理解tableView。快乐编码:)

于 2014-07-21T06:15:46.337 回答