ios - 单元格标签在表格滚动之前不会调整大小

标签 ios objective-c uitableview uilabel

我正在使用此代码来调整用于维护我的单元格标题的标签的大小。问题是在我滚动表格之前,单元格标签不会调整大小。单元格需要消失才能正确调整其标签的大小。我可以做些什么来调整所有单元格标签的大小而无需事先滚动?

感谢阅读。

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

    ExampleTableViewCell *cell = (ExampleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ExampleTableViewCell"];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ExampleTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.title.text = [self.test objectAtIndex:[indexPath row]];

    //Calculates the expected size based on the font and linebreak mode of the label.
    CGSize maximumLabelSize = CGSizeMake(200, [UIScreen mainScreen].bounds.size.width);

    CGSize expectedLabelSize = [cell.title.text sizeWithFont:cell.title.font constrainedToSize:maximumLabelSize lineBreakMode:cell.title.lineBreakMode];

    // Adjusts the label the the new height.
    CGRect newFrame = cell.title.frame;
    newFrame.size.height = expectedLabelSize.height;
    cell.title.frame = newFrame;

    return cell;
}

最佳答案

尝试在

中编写相同的代码

willDisplayCell UItableViewController 的方法

另一种方法是......在你的 ExampleTableViewCell 类中实现

- (void)layoutSubviews{
}

在上面的函数中设置标题的框架。它应该工作

关于ios - 单元格标签在表格滚动之前不会调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15785873/

相关文章:

ios - 2 UIViewController中的UITableView相互影响

ios - 边界在带有内容插入的 UIScrollView 上自动更改

ios - 在没有互联网时防止 JSONSerialization 崩溃我的应用程序的最佳方法?

iOS - map 用户位置未居中

ios - pkyeck socket.IO-objc 未向 socket.io 服务器发送事件

ios - UITableView 委托(delegate)使用扩展 swift

ios - 当通过 UIRefreshControl 调用时,UITableView 重新加载会重复 NSMutableArray 的值

ios - 如何组合过滤器

ios - 如何将 UPC-A 转换为 UPC-E?

objective-c - 从联系人中获取所有电子邮件地址 (iOS)