【问题标题】:Repeat Problem of tableview cell during scrolling in iphone在 iphone 中滚动时重复出现 tableview 单元格的问题
【发布时间】:2010-01-08 12:14:53
【问题描述】:

有 5 个单元格,每个单元格都有一个动态标签。使用 heightForRowAtIndexPath 方法根据标签内容定义单元格的高度。现在,正在显示 3 个单元格。当我滚动下一个单元格时,cellForRowAtIndexPath 会被调用一次,并且在输出中任何一个(4 或 5)单元格都正确显示,但另一个单元格有第一个单元格内容(重复)。 有什么解决方案可以解决这个问题吗? cellForRowAtIndexPath 方法应该被调用 2 次。 我已经正确定义了部分和行。

【问题讨论】:

  • 您可能没有正确重用单元格。请提供您的cellForRowAtIndexPath 代码示例。
  • 嗨,Brij,在这种情况下,最好的办法是使用定义的重用概念。您可以重复使用,只要您需要一个与所需匹配的 cellView。在您提出的正确答案中,您永远不会这样做。在我的回答中,我提供了一种方法,可用于避免为每一行创建一个新的 cellView,而是为每种 cellView 创建一个标识符。好的,在最坏的情况下,它会为每一行创建一个 cellView,但平均而言,您将获得比每次只创建新视图更好的性能。
  • 同一个表格视图被多次使用,不同的按钮点击不同的数据。数据不一样。我使用 tableview 作为模板,这就是我每次都需要重新创建单元格的原因。

标签: iphone objective-c uitableview


【解决方案1】:

删除判断

如果(单元格 == nil)

很好。我现在解决了我的问题。非常感谢。

【讨论】:

    【解决方案2】:

    您可能没有正确地重复使用单元格。当您滚动时,UITableView 会重复使用前一个单元格,但您没有正确更改其内容。

    cellForRowAtIndexPath 中的代码应如下所示:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *kCustomCellID = @"CustomCellID";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];
        if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCustomCellID] autorelease];
        }
    
        cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [self.dateFormatter stringFromDate:[NSDate date]];
    
        return cell;
    }
    

    你是这样的吗?

    【讨论】:

    • 是的,我正在使用 switch 语句根据 indexPath 添加标签。
    • 你能发布你的cellForRowAtIndexPath吗?
    【解决方案3】:

    由于每行没有固定的大小,您将无法重复使用它们,除非再次呈现相同的内容或一些类似的内容。

    我的建议是为每一行创建一个不同大小的标识符。

    您说您有一个基于 indexPath 添加内容的开关,因此您应该根据其大小将每个 case 语句中的可重用单元格出列。

    【讨论】:

      【解决方案4】:

      永远不要删除if(cell == nil)

      而是清除您在单元格上添加的子视图。

        if (cell == nil) {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault    reuseIdentifier:CellIdentifier] ;
             }else{
             NSArray *cellSubviews= [cell.contentView cellSubviews];
              for (UIView * view in subviews) {
                  [view removeFromSuperview];
              }
          }
      

      【讨论】:

        【解决方案5】:
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            NSString *CellIdentifier = [NSString stringWithFormat:@"cell %d",indexPath.row];
        
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
            if (cell == nil)
            {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        
                cell.textLabel.text = @"Hello";
        
            }
            // Configure the cell.
            return cell;
        }
        

        【讨论】:

          【解决方案6】:

          尝试为每个单元格指定一个唯一标识符,

          NSString *CellIdentifier =[NSString stringWithFormat:"Cell %d",indexpath.row];

          不知道这是否是正确的方法,但它应该可以解决你的问题。

          【讨论】:

            【解决方案7】:

            问题是单元格不是空的,所以它在滚动时多次返回相同的单元格。我从 cellForRowAtIndexPath 方法中删除了条件。

            if (cell == nil)
            

            现在每次都在分配单元格并且工作正常。

            【讨论】:

            • 请记住,(性能方面)这是您可以做的最糟糕的事情
            • 同一个表格视图被多次使用,不同的按钮点击不同的数据。让我知道在这种情况下应该怎么做,而不是多次调用 viewcontroller。
            猜你喜欢
            • 1970-01-01
            • 2020-02-14
            • 2021-07-18
            • 2011-02-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-05-02
            • 1970-01-01
            相关资源
            最近更新 更多