在滚动开始之前,UITableView的单元格中的标签不会被包装

时间:2015-01-14 17:13:40

标签: ios interface-builder autolayout

我有一个UITableView,其原型单元格内部有两个标签。在16个单元格中有16个不同大小的硬编码文本字符串(要点是它们要换行)。当视图最初加载时,我看到每个单元格中的第二个标签未被包装:

enter image description here

当我向下滚动时,文本被正确包裹:

enter image description here

当我向上滚动时,单元格被正确包裹..我可以发布更多信息(比如代码),但我希望解决方案对某人来说是显而易见的,而且没有必要。

这是在iOS 8中。

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

//  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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我还发现-(NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection:(NSInteger)section { return ((TableItem *)self.items[section]).count; } 是答案,但我认为只需要在初始单元格创建时调用一次。在我的情况下layoutIfNeeded,但如果你没有使用Interface Builder,其他地方可能是合适的。