gpt4 book ai didi

ios - 无法使 UITableView 单元格高度与文本量成比例

转载 作者:行者123 更新时间:2023-11-28 22:51:38 25 4
gpt4 key购买 nike

我正在尝试让 UITableView 单元格适合文本。到目前为止,我有这样的事情:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *label = nil;

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault)
reuseIdentifier:@"business"];

NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")];

cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f);

CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];


label = [[UILabel alloc] initWithFrame:CGRectZero];

[label setText:comment];
[label setFrame:CGRectMake(10, 10, 320 - (10 * 2), MAX(size.height, 44.0f))];

cell.textLabel.text = comment;
}

我也有这个功能

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")];

CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f);

CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

CGFloat height = MAX(size.height, 44.0f);

return height + (10 * 2);
}

但它没有为 UITableView 的单元格分配正确的高度。我认为我在分配标签和单元格文本的方式/位置上犯了一些错误。

请帮助我理解它应该如何。

谢谢!

最佳答案

这会让你们更亲近吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"business"];

if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault)reuseIdentifier:@"business"];
}

NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")];

CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f);

CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, MAX(size.height, 44.0f) + 20.0f)];
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
label.text = comment;

[cell.contentView addSubview:label];

}

关于ios - 无法使 UITableView 单元格高度与文本量成比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11851789/

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