简体   繁体   中英

Scroll UITableView to display selected cell

If I have a UITableView with a selected cell, how can I scroll the table so the selected cell is visible? (Or better, so that cell is in the middle of the screen).

Thanks!

有一种特殊的方法

[tableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionMiddle animated:YES];
if let indexPath = tableView.indexPathForSelectedRow {
    tableView.scrollToRowIndexPath(indexPath, atScrollPosition: .None, animated: true)
}

You should use (if you know your indexPath selected):

 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
 [yourTableView scrollToRowAtIndexPath:indexPath 
                     atScrollPosition:UITableViewScrollPositionTop 
                             animated:YES];

or

[yourTableView scrollToNearestSelectedRowAtScrollPosition: UITableViewScrollPositionMiddle animated:YES];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM