嗨,我在UILabel字段上使用sizetofit,当我加载tableview控制器时,它可以正常工作,但是当我上下滚动时,它开始剪切字母并将其换行。这是我在CellForRowAtIndexPath中使用的代码:

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

    // Configure the cell...

    // Load and display subject photo image
    UIImageView *subjectImageView = (UIImageView *)[cell viewWithTag:200];
    subjectImageView.image = [UIImage imageNamed:subjectImages[indexPath.row]];


    // Load and display subject label
    UILabel *subjectLabel = (UILabel *)[cell viewWithTag:201];
    subjectLabel.text = [subjectItems objectAtIndex:indexPath.row];
    [subjectLabel sizeToFit];// call this to fit size of the label according to text



    return cell;
}

这是我原来加载的tableviewcontroller的屏幕截图:

ios - UItableViewController SizeToFit不适用于UILabel字段-LMLPHP

在这里上下滚动后,您可以看到说明已被剪切

ios - UItableViewController SizeToFit不适用于UILabel字段-LMLPHP

感谢您为解决此问题提供的任何帮助。

干杯

卡森

最佳答案

sizeToFit用作UILabel可能会导致UITableView中的此类乱码,从而导致单元重用。我能想到的解决方案之一是在调用sizeToFit之前为标签宽度设置框架的宽度,并且每次将文本分配给标签时都必须这样做。它看起来应该像这样:

CGRect frame = subjectLabel.frame;
frame.size.width = 100.0; //try to adjust this value
subjectLabel.frame = frame;
[subjectLabel sizeToFit];

关于ios - UItableViewController SizeToFit不适用于UILabel字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32414857/

10-09 03:08