Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll to top in IOS7 UITableView?

In IOS6 I have the following code to scroll to the top of a UITableView

[tableView setContentOffset:CGPointZero animated:YES]; 

In IOS7 this doesn't work anymore. The table view isn't scrolled completely to the top (but almost).

like image 210
Markus Johansson Avatar asked Oct 08 '13 08:10

Markus Johansson


People also ask

How do I scroll the header along with UITableView?

If you have a single header in the table then you can use tableHeaderView as below: tableView. tableHeaderView = Header; Or if you have multiple header in table than you need to use Group table instead of plain table.

How do I scroll to top tableView?

To scroll to the top of our tableview we need to create a new IndexPath . This index path has two arguments, row and section . All we want to do is scroll to the top of the table view, therefore we pass 0 for the row argument and 0 for the section argument. UITableView has the scrollToRow method built in.

Can UITableView scroll horizontal?

You can add a UITableView to a UIScrollView and have it scroll horizontally.

What is tableView?

A table view displays a single column of vertically scrolling content, divided into rows and sections. Each row of a table displays a single piece of information related to your app.


2 Answers

In iOS7, whole screen UITableView and UIScrollView components, by default, adjust content and scroll indicator insets to just make everything work. However, as you've noticed CGPointZero no longer represents the content offset that takes you to the visual "top".

Use this instead:

self.tableView.contentOffset = CGPointMake(0, 0 - self.tableView.contentInset.top); 

Here, you don't have to worry about if you have sections or rows. You also don't tell the Table View to target the first row, and then wonder why it didn't show all of your very tall table header view, etc.

like image 103
idStar Avatar answered Oct 02 '22 13:10

idStar


Try this:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
like image 37
Harikrishnan Avatar answered Oct 02 '22 13:10

Harikrishnan