开发者

Can I have cells with different hight in an UITableView?

开发者 https://www.devze.com 2022-12-26 13:32 出处:网络
I need to present some information in a table view where some information is pretty flat, and other is 开发者_如何学运维pretty big. i.e. one information is just a line of text really, while the other

I need to present some information in a table view where some information is pretty flat, and other is 开发者_如何学运维pretty big. i.e. one information is just a line of text really, while the other is three lines of text.

From what I know, UITableView asks for the hight of all cells. So is there any way to have cells with distinct heights?


Yes, see:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

Example implementation:

NSString *string...;
CGFloat CellWidth...;
CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:14.0] 
               constrainedToSize:CGSizeMake(textWidth, MAXFLOAT) 
                   lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = size.height;
if (height + 13.0) < 44.0) {
    return 44.0;
}
return height + 13.0;


I agree with paull, but you could also subclass if you wanted something extra than just resizing the height. Just think about what else you may need. Good luck, cocoa can be a real pain sometimes.

0

精彩评论

暂无评论...
验证码 换一张
取 消