Gnamm
Gnamm

Reputation: 633

Scroll UITableView in iOS7

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];

Upvotes: 2

Views: 3327

Answers (2)

Hussain KMR Behestee
Hussain KMR Behestee

Reputation: 852

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

Upvotes: 0

Daniel
Daniel

Reputation: 23359

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];

Upvotes: 1

Related Questions