iphone - 快速滚动时,自定义单元格中的多行标签与下一个单元格重叠

标签 iphone objective-c ios uitableview

我发现类似的问题几乎被问了几十次,但没有一个答案解决了我的问题(或者我太累了,所以我没有正确遵循答案)

我有一个带有自定义单元格的表格 View 。

单元格看起来像这样

My custom cell in storyboard

单元格的类型为 ResultsViewCell,它派生自 UITableViewCell。

单元格中的最后一个标签是多行单元格。该单元格有时会与下一个单元格的内容重叠。

这是我的 cellForRowAtIndexPath 函数

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

        ResultsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        // Configure the cell...
        Data *theData = [Data getInstance];
        Company *theCompany = [theData.results objectAtIndex:indexPath.row];

        cell.lblTitle.text = theCompany.DisplayName;

        cell.lblDescription.text = theCompany.Description;
        cell.lblAddressPt1.text = theCompany.AddressPt1;
        cell.lblAddressPt2.text = theCompany.AddressPt2;
        cell.lblPhone.text = theCompany.Phone;
        cell.lblEmail.text = theCompany.Email;

        cell.lblDescription.adjustsFontSizeToFitWidth = false;
        cell.lblDescription.lineBreakMode = UILineBreakModeWordWrap;
        cell.lblDescription.numberOfLines = 0;
        [cell.lblDescription sizeToFit];

       ///////////////////////////////////////////
       //edit -Added after David H's answer, but it didn't solve the problem
       cell.contentView.clipsToBounds = false;
       UIFont *cellFont = [UIFont systemFontOfSize:12.0];
       CGSize constraintSize = CGSizeMake(270.0f, MAXFLOAT);
       CGSize labelSize = [theCompany.Description sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
       [cell.contentView setFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, 270, 90 + labelSize.height)];
       //end of edit
       ///////////////////////////////////////

        return cell;
    }


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    Data *theData = [Data getInstance];
    Company *theCompany = [theData.results objectAtIndex:indexPath.row];

    UIFont *cellFont = [UIFont systemFontOfSize:12.0];
    CGSize constraintSize = CGSizeMake(270.0f, MAXFLOAT);
    CGSize labelSize = [theCompany.Description sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return (90.0f + labelSize.height);
}

我做错了什么?

最佳答案

这可能会让您感到惊讶,但 UIView 属性“clipsToBounds”的默认值是“否” - 也就是说,如果 subview 超出 View 的框架,则无论如何都会显示它们。

解决方法是确保这些 View 中没有一个将“clipsToBounds”设置为“NO”。您应该在 cell.contentView 上将其设置为 YES,并确保 cell.contentView 的框架具有与您在委托(delegate)“heightForRowAtIndexPath:”方法中报告的高度相同的高度。

您可能还需要在多行标签上将“clipsToBounds”设置为 YES(不确定,可能不是)。

关于iphone - 快速滚动时,自定义单元格中的多行标签与下一个单元格重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12379401/

相关文章:

iphone - iPhone中填充模型类的正确做法

objective-c - 写入包中的文件?

ios - 找不到“JWPlayer-iOS-SDK/JWPlayerController.h”文件

ios - 如何使用 SKEmitterNode 效果像 SKShapeNode 一样绘制?

ios - 如何计算iOS应用程序启动时间

ios - 如何配置 Pods 项目的build设置?

iphone - 处理空 TableView

iphone - 为什么 iPhone 应用程序更新会破坏我的应用程序?

iphone - 词典应用

ios - 如何在 Swift 代码中正确使用 #ifdef DEBUG/RELEASE 值?