在滚动开始之前,UITableView 单元格中的标签不会换行

Labels in UITableView's cells are not wrapped until scrolling starts

我有一个带有原型单元格的 UITableView,其中有两个标签。 16 个单元格中有 16 个 hard-coded 大小不等的文本字符串(重点是让它们换行)。最初加载视图时,我看到每个单元格中的第二个标签未换行:

当我向下滚动时,文本被正确换行:

当我向上滚动时,单元格被正确包装。我可以 post 更多信息(如代码),但我希望解决方案对某些人来说是显而易见的,这不是必需的。

这是在iOS8.

**更新 - 这是一些代码! **

//  TestViewController.h
#import <UIKit/UIKit.h>

@interface TestViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@end

//  TestViewController.m
#import <Foundation/Foundation.h>
#import "TestViewController.h"
#import "MessageCell.h"

@interface TestViewController ()

@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSArray *messages;

@property IBOutlet UITableView *tableView;

@end

@implementation TestViewController

-(void) viewDidLoad {
    [super viewDidLoad];

    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.estimatedRowHeight = 66.0;

    self.tableView.rowHeight = UITableViewAutomaticDimension;

    self.titles = @[
                    @"One",
                    @"Two Two",
                    @"Three Three Three",
                    @"Four Four Four Four",
                    @"Five Five Five Five Five",
                    @"Six Six Six Six Six Six",
                    @"Seven Seven Seven Seven Seven Seven Seven",
                    @"Eight Eight Eight Eight Eight Eight Eight Eight",
                    @"Nine",
                    @"Ten",
                    @"Eleven",
                    @"Twelve",
                    @"Thirteen",
                    @"Fourteen",
                    @"Fifteen",
                    @"Sixteen",
                    ];
    self.messages = @[
                      @"This is a short message.",
                      @"This is a long message. It's longer than the medium sized message so we can see how it looks with the wrapping and dynamic sized cells.",
                      @"This is a medium sized message. It's not going to be very long.",
                      @"This is a long message. It's longer than the medium sized message so we can see how it looks with the wrapping and dynamic sized cells.",
                      @"This is a short message.",
                      @"This is a long message. It's longer than the medium sized message so we can see how it looks with the wrapping and dynamic sized cells.",
                      @"This is a medium sized message. It's not going to be very long.",
                      @"This is a long message. It's longer than the medium sized message so we can see how it looks with the wrapping and dynamic sized cells.",
                      @"This is a short message.",
                      @"This is a long message. It's longer than the medium sized message so we can see how it looks with the wrapping and dynamic sized cells.",
                      @"This is a medium sized message. It's not going to be very long.",
                      @"This is a long message. It's longer than the medium sized message so we can see how it looks with the wrapping and dynamic sized cells.",
                      @"This is a short message.",
                      @"This is a long message. It's longer than the medium sized message so we can see how it looks with the wrapping and dynamic sized cells.",
                      @"This is a medium sized message. It's not going to be very long.",
                      @"This is a short message.",
                      ];

}

- (void)viewWillAppear:(BOOL)animated
{
    [[self navigationController] setNavigationBarHidden:NO animated:NO];
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"in number of rows. returning: %ul", [self.messages count]);
    return [self.messages count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"in cell for row: %ld", (long)indexPath.row);

    NSString *cellIdentifier = @"MessageCell";
    MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[MessageCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:cellIdentifier];
    }

    cell.title.text = [self.titles objectAtIndex:indexPath.row];
    cell.message.text = [self.messages objectAtIndex:indexPath.row];


    return cell;
}

@end

我发现了一个类似的问题here

解决方案是确保我在 table 视图控制器中的 cellForRowAtIndexPath 中调用 [cell.contentView layoutIfNeeded];

我还发现 layoutIfNeeded 是答案,但我认为它只需要在初始单元格创建时调用一次。在我的例子中 awakeFromNib 但如果你不使用 Interface Builder,其他地方可能是合适的。