gpt4 book ai didi

ios - UILabels 未在 UITableView 中调整大小

转载 作者:行者123 更新时间:2023-11-29 13:13:01 25 4
gpt4 key购买 nike

我有一个使用原型(prototype)单元格的 UITableView。这些单元格有一个名为 dataCell 的自定义类。自定义单元格也有三个 UILabel:idLabel、contLabel 和 expLabel。单元格根据 expLabel 中的文本量适当调整大小;但是,我无法让标签本身调整大小。当我向下滚动时,一些标签会调整大小;但是,当我向上滚动时,它们也会恢复为仅显示两行并省略文本。这是我的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

dataCell *cell = (dataCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];


// the rest of your configure cell

// First Cell Label
[cell.idLabel setText:[idData objectAtIndex:indexPath.row]];
cell.idLabel.numberOfLines = 0;


// Second Cell Label
[cell.contLabel setText:[conData objectAtIndex:indexPath.row]];
cell.contLabel.numberOfLines = 0;



// Third Cell Label
[cell.expLabel setText:[expData objectAtIndex:indexPath.row]];
cell.expLabel.numberOfLines=0;
CGSize expectedLabelSize = [cell.expLabel.text sizeWithFont:cell.expLabel.font constrainedToSize:CGSizeMake(220, FLT_MAX) lineBreakMode:cell.expLabel.lineBreakMode];
cell.expLabel.frame=CGRectMake(cell.expLabel.frame.origin.x, cell.expLabel.frame.origin.y, expectedLabelSize.width, expectedLabelSize.height);




return cell;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
dataCell *cell = (dataCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
CGSize maximumLabelSize = CGSizeMake(220, FLT_MAX);
[cell.expLabel setText:[expData objectAtIndex:indexPath.row]];
CGSize expectedLabelSize = [cell.expLabel.text sizeWithFont:cell.expLabel.font constrainedToSize:maximumLabelSize lineBreakMode:cell.expLabel.lineBreakMode];
if (expectedLabelSize.height<43) {
expectedLabelSize.height=43;
}

return expectedLabelSize.height; }

任何帮助将不胜感激

最佳答案

如果您使用的是 Storyboard和 UITableViewCell,那么您只需更改自动调整大小的掩码,但如果您以编程方式进行,则必须设置计算文本的宽度和高度并重置标签的框架,

UILabel* label = [[UILabel alloc] init];
UIFont* font = label.font;
CGSize maxContentSizeForText = CGSizeMake(maxTextWidth, maxTextHeight);
CGSize stringTextSize = [string sizeWithFont:font constrainedToSize:maxContentSizeForText lineBreakMode:NSLineBreakByWordWrapping];
[label setFrame:CGRectMake(xPosition, yPosition, stringTextSize.width, stringTextSize.height);
[label setNumberOfLines:1000];

您的标签可能是 xib 文件或 Storyboard的属性,行数只是表示您希望标签变得非常非常大,因为我们不能说“无限”,我通常使用 1000 表示最多 1000 行文本

关于ios - UILabels 未在 UITableView 中调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16722364/

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