如何创建这样的表格单元格布局?

时间:2013-03-16 17:40:20

标签: iphone cocoa-touch uitableview

我想做的就是创建一个UITableViewCell,其布局只有两个元素:

"Text that can have a flexible length 
with mutliple lines..."
"one fix line directly follwing the above text"

所以基本上 1.具有可变长度的灵活文本字段,如果需要,必须在多行中包含完整文本 2.一行直接跟随

虽然我发现heightForRowAtIndexPath但它仍然没有帮助如何在这里进行第二个字段的布局......

   -(void) layoutSubviews {
    [super layoutSubviews];
    [flexibleText setFrame:CGRectMake(  5.0,  5.0, 250.0, 40.0)];  //<- fexible height
        [one_line setFrame:CGRectMake(  5.0, 42.0, 250.0, 20.0)]; //<- followed by this one liner
}

1 个答案:

答案 0 :(得分:1)

您必须为UITableViewCell创建子类,然后在布局中计算第一个标签需要的大小。请查看此What is -[NSString sizeWithFont:forWidth:lineBreakMode:] good for?了解如何使用-[NSString sizeWithFont:constrainedToSize:lineBreakMode:]

然后根据第一个标签重新定位第二个标签的框架。

然后,您可以创建一个方法,该方法将返回单元格所需的高度,并在heightForCell方法中调用该高度。

相关问题