当单元格包含带有换行符的uilabel时,heightForRowAtIndexPath不正确

时间:2013-10-21 19:43:22

标签: ios uitableview

我想在uitableviewcell中显示地址信息,并使用自动布局的自定义单元格。我想,我没有单独的地址字段标签,我会有一个标签,并将地址字段连接在一起,并使用换行符“\ n”,以便每个地址字段出现在标签的新行,然后我将标签行数设置为零,以便自动扩展。

那部分工作正常,什么是不行的是当我尝试计算heightForRowAtIndexPath时,它报告的标签大小不足以使地址被截断。

e.g。这里提取了一些代码:看起来boundingRectWithSize似乎没有考虑到换行符?无论如何,我很想不使用单个标签,而是有单独的字段,这将更多的工作,但我认为它不会遇到这些问题。

        NSString *description= [self getAddressDescription:_delegate.shoppingCart.shippingAddress]; // formats address fields into single string with newline characters 
NSAttributedString *attributedText =
                [[NSAttributedString alloc]
                        initWithString:description
                            attributes:@
                            {
                                    NSFontAttributeName: [ViewHelper getDescriptionFont]
                            }];

CGRect rect = [attributedText boundingRectWithSize:(CGSize){200, CGFLOAT_MAX}
                                                   options:NSStringDrawingUsesLineFragmentOrigin
                                                   context:nil];
CGSize descriptionSize = rect.size;
return kParentChildDefaultSpace + + descriptionSize.height + kParentChildDefaultSpace;

1 个答案:

答案 0 :(得分:0)

您可以将原型单元格方法described here[label sizeToFit]结合使用。有一个有效的sample project here