This morning I just installed new Xcode which includes iOS 6.
I have a table view loaded with a plist file containing chapters and lines. Chapters define the section
Fyodor Volchyok's answer in Swift:
tableView.reloadData()
let indexPath = NSIndexPath(forRow: linePos, inSection: chapterPos)
// make sure the scroll is done after data reload was finished
dispatch_async(dispatch_get_main_queue()) {
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
}
Objective-C
[self.tableView reloadData];
dispatch_async(dispatch_get_main_queue(), ^{
NSIndexPath *rowIndexPath = [NSIndexPath indexPathForRow:3 inSection:0];
[self.tableView scrollToRowAtIndexPath:rowIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
});
Swift
tableView.reloadData()
DispatchQueue.main.async {
let indexPath = IndexPath(row: linePos, section: chapterPos)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}
For recent versions of iOS, please read Fyodor Volchyok's answer. Note that it's not marked as the accepted answer simply because at the time the question was first asked (Sept. 2012), the current answer was the working solution. More recent versions of iOS also got the same problem which is now solved by Fyodor Volchyok's answer, so you should +1 his answer at that moment.
I found the answer. I have to first reload the data in the tableview
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:linePos inSection:chapterPos];
[self.tableView reloadData];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop animated:YES];
Even though I found the answer I don't know why it is working in iOS5 and not in iOS6.
EDIT
Perhaps I should add that even though it was working, I was still having a problem in displaying the last row and posted a question for that
UItableview scrollToRowAtIndexPath not displaying last row correctly
As @Raj also asked for it, I should say that I was triggering that in the viewDidLoad
. To correct the problem of the last row not displaying correctly I had to put it in the viewDidAppear
.