ios - 最后一行的 UITableView indexPath

标签 ios swift uitableview

我正在尝试使 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/

相关文章:

ipad - iPad:遍历UITableView中的每个单元格吗?

iOS swift : Fatal error with unwrapping optional in UICollectionView

ios - 停止 UISearchDisplayController 显示空单元格

ios - View 标签属性的 switch 语句

ios - Xcode View 层次结构调试器 - "prototyping"

ios - 将动态数组传递给 ImageSlideshow ios

swift - "if not case"用于 Swift 中的枚举

objective-c - 如何改变rootViewController的 View ?

iphone - 设备关闭时忽略本地通知

ios - 检测 iOS 应用程序何时首次启动?