iOS UItableview scrollToRowAtIndexPath not working

2019-02-05 23:29发布

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 sections.

The user selects chapter and line and the tableview is automatically scrolled to the correct position (in the viewDidLoad).

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:linePos inSection:chapterPos];
[self.tableView scrollToRowAtIndexPath:indexPath 
    atScrollPosition:UITableViewScrollPositionTop animated:YES];

this works just great in iOS5 simulator.

Trying this in the iOS 6 simulator the scroll is not performed. I get no errors. I have checked, linePos and chapterPos receive correct values but the scroll is not performed.

Any ideas why ?

7条回答
▲ chillily
2楼-- · 2019-02-06 00:17

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)
}
查看更多
登录 后发表回答