gpt4 book ai didi

iphone - 如何让 UITableView 行高自动调整为 UITableViewCell 的大小?

转载 作者:IT王子 更新时间:2023-10-29 08:01:55 24 4
gpt4 key购买 nike

如何让 UITableView 的行高自动调整为 UITableViewCell 的大小?

所以假设我在 Interface Builder 中创建 UITableViewCell,并且它的高度超过标准尺寸,我怎样才能让 UITableView 行高自动调整到它的大小? (即与必须在界面构建器中手动测量高度并以编程方式设置高度相反)

最佳答案

http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/是一个很好的教程。

主要是

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
// Get the text so we can measure it
NSString *text = [items objectAtIndex:[indexPath row]];
// Get a CGSize for the width and, effectively, unlimited height
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
// Get the size of the text given the CGSize we just made as a constraint
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
// Get the height of our measurement, with a minimum of 44 (standard cell size)
CGFloat height = MAX(size.height, 44.0f);
// return the height, with a bit of extra padding in
return height + (CELL_CONTENT_MARGIN * 2);
}

关于iphone - 如何让 UITableView 行高自动调整为 UITableViewCell 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5161782/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com