我正在尝试使 UITableView
中的最后一行在添加后可见。现在,当我添加一行并调用 reloadData 时,表格会转到顶部。
我想如果我得到最后一行的 indexPath
,我就可以选择该行并且它应该出现在列表中。我不确定如何获得该值,或者即使我正在正确地接近它。
如何获取特定行的 indexPath
?
最佳答案
请注意,您不需要调用 reloadData
来使最后一行可见。您可以使用 scrollToRowAtIndexPath
方法。
您可以使用以下代码来实现您的目标。
// First figure out how many sections there are
let lastSectionIndex = self.tblTableView!.numberOfSections() - 1
// Then grab the number of rows in the last section
let lastRowIndex = self.tblTableView!.numberOfRowsInSection(lastSectionIndex) - 1
// Now just construct the index path
let pathToLastRow = NSIndexPath(forRow: lastRowIndex, inSection: lastSectionIndex)
// Make the last row visible
self.tblTableView?.scrollToRowAtIndexPath(pathToLastRow, atScrollPosition: UITableViewScrollPosition.None, animated: true)
swift 4.0:
tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.none, animated: true)
关于ios - 最后一行的 UITableView indexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31308113/