【问题标题】:Cells are overlapping单元格重叠
【发布时间】:2014-12-17 16:00:52
【问题描述】:

我的 iPhone 应用程序有问题。有时,我的主屏幕的 UITableView 的某些单元格重叠并且显示不正确。

以下是我的应用程序有时(很少)出现的方式:

我创建了两种类型的自定义 UITableViewCell,并使用 xib 文件来设计它们。

您对导致这种情况的原因有任何想法吗?这是iOS的bug吗,因为我已经在其他应用中注意到了这种bug。

感谢您的帮助。

更新:这是我的 tableView:heightForRowAtIndexPath: 方法。

我根据两种类型的单元格返回正确的高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Article *article = [self.articles objectAtIndex:[indexPath row]];

    if (article.isFeatured) {
        return 110;
    } else {
        return 75;
    }
}

【问题讨论】:

  • 您是否在使用自动调整单元格大小?还是您从-tableView:heightForRowAtIndexPath: 返回一个值?如果您使用自动调整大小,则可能需要修复您的约束。
  • 正如@ZevEisenberg 提到的,单元格高度似乎有些问题,因为很少有单元格的高度比正常单元格高。
  • 这不是错误。您为其中一种单元格类型返回了错误的单元格高度。如果您使用自动布局,请发布您的 tableView:heightForRowAtIndexPath: 方法并编辑您的问题以包括在内。
  • 使用自动布局调整单元格大小。
  • 我添加了我的 heightForRowAtIndexPath 方法。是的,这是一个错误(可能是我的代码,也可能是 iOS,我不知道,这就是我问的原因),因为它很少发生。大多数情况下(99% 的时间......),一切都正确显示。

标签: ios objective-c cocoa-touch uitableview ios8


【解决方案1】:

除非您使用单元格的高度覆盖 -tableView:heightForRowAtIndexPath:,否则所有单元格的标准高度将是 44 磅。

在您的 TableView 委托中,您需要拥有

- (CGFloat)tableView:(UITableView *) tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    CGFloat cellHeight = 44.0;

   //Calculate your cell height using indexPath and save it in cellHeight;
   // Then

   return cellHeight;
}

【讨论】:

  • 我正在覆盖 -tableView:heightForRowAtIndexPath: 我更新了我的答案。
【解决方案2】:

试试这个

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

UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];

if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}    

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 30.0f;
}

【讨论】:

    【解决方案3】:

    我认为您可以根据内容动态计算单元格高度。

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        CGSize constrainedSize = CGSizeMake(self.view.frame.size.width - 30, FLT_MAX);
    
        NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                              [UIFont fontWithName:@"HelveticaNeue" size:14], NSFontAttributeName,
                                              nil];
    
        CGRect requiredHeight = [self.articles objectAtIndex:[indexPath row] boundingRectWithSize:constrainedSize
                                                                          options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                                                       attributes:attributesDictionary
                                                                          context:nil];
    
        return requiredHeight.size.height + 20;
    }
    

    【讨论】:

    • 为什么?有两种类型的单元格,两种类型的单元格具有静态高度(不同,但静态)。
    【解决方案4】:

    对于那些来到这里的人,我在 iOS 11 中遇到了完全相同的问题,而且这种情况也很少发生。事实证明,我在表格视图中进行了一些动画编辑,但我遇到了两个问题:有时这些更新发生在放置此表格视图的控制器不在屏幕上时,因此表格视图没有窗口;其他时候,视图控制器在屏幕上,但动画在 UIView.performWithoutAnimation 块内(由于重构不当),如下所示:

    UIView.performWithoutAnimation {
        tableView.beginUpdates()
        // a couple of calls to tableView.insertRows(at:with:) 
        // and tableView.deleteRows(at:with:)
        tableView.endUpdates()
    }
    

    就我而言,我删除了UIView.performWithoutAnimation 并再次启用了动画,但如果您需要保持表格更新而不使用动画,我建议您删除对tableView.beginUpdates()tableView.endUpdates() 的调用并进行更新带有动画.none,或者直接调用tableView.reloadData()。 我还检查了包含此表视图的视图控制器是否已加载视图及其view.window != nil,如果没有,我只需执行tableView.reloadData() 而不是动画编辑。可能会检查tableView.window != nil 是否会这样做。

    我希望这对某人有帮助:)

    【讨论】:

      猜你喜欢
      • 2016-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多