快速滚动时自定义单元格中的多行标签与下一个单元格重叠 [英] Multiline label in custom cell overlaps the next cell when scrolling fast

查看:26
本文介绍了快速滚动时自定义单元格中的多行标签与下一个单元格重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到类似的问题被问了几十次,但没有一个答案能解决我的问题(或者我太累了所以我没有正确回答)

I see that similar question is asked almost dozens of times, but none of the answers solved my problem (or I'm just too tired so I'm not following answers correctly)

我有一个带有自定义单元格的表格视图.

I have a tableview with custom cell.

单元格看起来像这样

单元格是从 UITableViewCell 派生的 ResultsViewCell 类型.

The cell is of type ResultsViewCell, which is derived from UITableViewCell.

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

Last label in the cell is multiline cell. That one sometimes overlaps contents of the next cell.

这是我的 cellForRowAtIndexPath 函数

Here's my cellForRowAtIndexPath function

    - (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' 的默认值是 NO - 也就是说,如果子视图超出了视图的框架,则显示它们无论如何.

It might surprise you, but the default for the UIView property 'clipsToBounds' is NO - that is, if subviews extend past the frame of the view, show them anyway.

修复方法是确保这些视图中没有一个将 'clipsToBounds' 设置为 NO.您应该在 cell.contentView 上将其设置为 YES,并确保 cell.contentView 的框架与您在委托 'heightForRowAtIndexPath:' 方法中报告的高度相同.

The fix is to insure that none of these views have 'clipsToBounds' set to NO. You should set it on cell.contentView to YES, and make sure that the frame of cell.contentView has the same height as you report back in the delegate 'heightForRowAtIndexPath:' method.

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

You may also need to set the 'clipsToBounds' to YES on the multiline label (not sure, probably not).

这篇关于快速滚动时自定义单元格中的多行标签与下一个单元格重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆