3

I have an interesting problem. I have a very basic tableview with a bunch of cells of varying height. It's using the iOS 8 UITableViewAutomaticDimension and auto layout constraints.

@interface ViewController () <UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *sentences;
@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.estimatedRowHeight = 44;
    self.tableView.rowHeight = UITableViewAutomaticDimension;

    self.sentences = @[
        @"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        @"Maecenas consectetur libero viverra sem viverra finibus.",
        @"Nunc arcu orci, pellentesque vel sapien nec, lobortis pulvinar magna"
    ];
    // and a bunch more, see example project

    [self scrollToBottom];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.sentences count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SentenceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.sentence = self.sentences[indexPath.row];
    return cell;
}

- (void)scrollToBottom {
    NSUInteger index = [self.sentences count] - 1;
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
}

So I want to start scrolled all the way to the bottom, and you have to scroll up to see older stuff. It's for a chat client basically. There are 2 problems: 1, it doesn't actually scroll to the bottom, and 2, when you scroll up, the cells shift position making scrolling jarring and weird. Not smooth at all.

Example project demonstrating the problem: https://github.com/kevinrenskers/AutoHeightFromBottom. Also see https://github.com/smileyborg/TableViewCellWithAutoLayoutiOS8/issues/8.

How do I solve this problem? Manually calculate cell height like in the olden days? Switch to collection view? Would that even solve anything?

Kevin Renskers
  • 5,156
  • 4
  • 47
  • 95
  • This is a duplicate of [iOS 8 Auto cell height - Can't scroll to last row](http://stackoverflow.com/questions/25686490/ios-8-auto-cell-height-cant-scroll-to-last-row) – smileyborg Sep 24 '14 at 13:29
  • You are completely right, didn't find it when searching for this problem. Voted to close my question as well. – Kevin Renskers Sep 24 '14 at 13:46
  • I'm facing same issue. Did you find any solution? – Ahmad Raza Jan 02 '15 at 14:27
  • Flip the tableview and flip the cells. You'll still have some weird jumping problems when scrolling up and then down again, but it's a whole lot better. I'm actually using [SlackTextViewController](https://github.com/slackhq/SlackTextViewController) for the chat window now, uses this trick and is pretty awesome. – Kevin Renskers Jan 02 '15 at 15:02

0 Answers0