2

Anyone know how to scroll an UITableView on iOS7?

I used to use this code and it worked very well but now seems that something is changed with contentSize (I had a problem like this with a textView)

[self.tableView scrollRectToVisible:CGRectMake(0, 0, self.tableView.contentSize.width, self.tableView.contentSize.height) animated:YES]

EDIT:

My code is like this

viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

keyboardWillShow:

[self.tableView scrollRectToVisible:CGRectMake(0.0, self.tableView.contentSize.height - 1.0, 1.0, 1.0) animated:NO];

Error:

I didn't notice the scroll when the keyboard appeared because I didn't update the contentSize.

Solution (based on Daniel answer)

CGSize size = self.tableView.contentSize;
size.height += keyboardBounds.size.height;
self.tableView.contentSize = size;

[self.tableView scrollRectToVisible:CGRectMake(0.0, self.tableView.contentSize.height - 1.0, 1.0, 1.0) animated:NO];
Gnamm
  • 633
  • 2
  • 8
  • 17
  • What is/isn't happening when you run that code? – Fogmeister Sep 19 '13 at 16:03
  • 1
    OK, but define nothing. Are you running it when you are at the top of the table? Or is it scrolled through? What is the content size? There is obviously a difference between what is happening and what you expect to happen. But saying, "it doesn't do what I want" isn't helpful. – Fogmeister Sep 19 '13 at 16:35

2 Answers2

1

In theory your code shouldn't do anything. You're scrolling to a rect which is the size of the tableview's content !

Because the rect you're using is already visible since it's the entire table view basically.

If you want to scroll to the bottom you should do this:

[self.tableView scrollRectToVisible:CGRectMake(0.0, 
                                               self.tableView.contentSize.height - 1.0, 
                                               1.0, 
                                               1.0) 
                           animated:YES];
Daniel
  • 23,129
  • 12
  • 109
  • 154
  • Is scrolling to the bottom what you are trying to do? Please make sure you're calling this after viewDidLoad and everything is setup as you expect. Maybe you're calling this too soon. – Daniel Sep 19 '13 at 16:34
0

For the TableView there is different method to scroll

[self.tableView scrollToRowAtIndexPath:desireRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

To view the more details on this you have a look on this

Community
  • 1
  • 1