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

时间:2012-09-11 23:42:13

标签: iphone objective-c ios uitableview

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

我有自定义单元格的桌面视图。

单元格看起来像这样

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);
}

我做错了什么?

3 个答案:

答案 0 :(得分:1)

你可能会感到惊讶,但是UIView属性'clipsToBounds'的默认值是NO - 也就是说,如果子视图延伸到视图的框架之外,无论如何都要显示它们。

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

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

答案 1 :(得分:0)

您需要根据该标签的文字高度调整tableViewCell高度。例如,采用这种方法:

- (int)textHeight:(NSString *)text {
    int width = self.tableView.frame.size.width;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 30)];
    label.numberOfLines = kMyNumberOfLines;

    //setup the label to look like the label of the cell

    label.text = text;
    [label sizeToFit];

    int h = (int)label.frame.size.height;
    [label release];
    return h;
}

然后在heightForRowAtIndexPath中使用该方法来确定细胞的高度。

答案 2 :(得分:0)

按照这样的方式......

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *myCell=[tableView dequeueReusableCellWithIdentifier:@"hi"];
    myCell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"hi"];

    if (myCell==nil)
    {
        NSArray *viewsToRemove = [mytable1 subviews];

        for (UITableView *table in viewsToRemove) 
        {
            [table removeFromSuperview];
        }
    }    
    return myCell;
}
相关问题