0

I have a UITableView with a prototype cell that has two labels inside of it. There are 16 hard-coded text strings of various size in 16 cells (the point is for them to wrap). When the view loads originally, I see the second label in each cell is not wrapped:

enter image description here

When I scroll down, the text is wrapped correctly:

enter image description here

When I scroll back up, the cells are wrapped correctly.. I can post more info (like code) but I'm hoping the solution is obvious to someone and that's not necessary.

This is in iOS 8.

**Update - here's some code! **

//  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
Chris Williams
  • 11,647
  • 15
  • 60
  • 97

2 Answers2

1

I found a similar question here.

The solution is to make sure I call [cell.contentView layoutIfNeeded]; in cellForRowAtIndexPath in my table view controller.

Community
  • 1
  • 1
Chris Williams
  • 11,647
  • 15
  • 60
  • 97
0

I also found that layoutIfNeeded was the answer but I think it only needs to be called once on initial cell creation. In my case in awakeFromNib but somewhere else may be appropriate if you aren't using Interface Builder.

Joseph Lord
  • 6,446
  • 1
  • 28
  • 32