user2402616
user2402616

Reputation: 1563

UITableView not scrolling to bottom

Hi I'm going to link you a video showcasing my problem as it's somewhat hard to convey with words. UITableView not scrolling to bottom In the video I'm hitting a button which adds items to the list. Note that I am not stopping at Item 16, and that I am still adding more items to the list that don't get displayed. Eventually, upon added new items, the list will automatically scroll up via

[_items addObject:[NSString stringWithFormat:@"Trawl %d", count]];

[self.tbl reloadData];

[self.tbl scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:self.items.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];

The main issue is towards the end of the video, where I scroll towards the last few items, but I cannot select them. Apparently the issue has to deal with the height of the UITableView. I've tried changing this in the storyboard editor but it didn't do anything. Any ideas? Thanks in advance!

Upvotes: 9

Views: 14659

Answers (3)

Cynichniy Bandera
Cynichniy Bandera

Reputation: 6103

Seems most of you are wrong.

It should be like this:

[self.tbl scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:self.items.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

That is, should be UITableViewScrollPositionTop, not UITableViewScrollPositionBottom. Here atScrollPosition:UITableViewScrollPositionTop is the origin of change, not the destination.

Upvotes: 0

Nic Robertson
Nic Robertson

Reputation: 1208

Ok from what I can see and from the comments, I think it is likely that the height of your UITableView is larger than the device screen size, which means it will not scroll down because the items haven't reached the bottom of the UITableView's UIScrollView.

Go to IB and set constraints so the UITableView is equal to the view controllers width and height.

Upvotes: 22

Abhi Beckert
Abhi Beckert

Reputation: 33359

Most likely reloadData doesn't fully execute when it gets called, because it assumes you might make more changes and tell it to reload again.

Try introducing a zero second delay, to give it a chance to run:

[_items addObject:[NSString stringWithFormat:@"Trawl %d", count]];

[self.tbl reloadData];

double delayInSeconds = 0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
  [self.tbl scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:self.items.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
});

Also, you should not be using reloadData in this situation. This is how rows should be added:

[_items addObject:[NSString stringWithFormat:@"Trawl %d", count]];

[self.postsTableView beginUpdates];
[self.postsTableView insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:_items.count - 1] withAnimation:NSTableViewAnimationEffectNone]; // maybe you want some other animation here
[self.postsTableView endUpdates];

double delayInSeconds = 0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
  [self.tbl scrollToRowAtIndexPath:[NSIndexPath indexPathForItem:self.items.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
});

Upvotes: 6

Related Questions