最后一行的UITableView indexPath

Sco*_*urn 12 uitableview ios swift

UITableView在添加之后,我试图在可见的最后一行.现在,当我添加一行并调用reloadData时,该表将显示在顶部.

我想如果我得到indexPath最后一行,我可以选择该行,它应该出现在列表中.我不确定如何获得这个价值,或者即使我正确地接近这个价值.

如何获取indexPath特定行?

Sha*_* TK 29

请注意,您无需调用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)
Run Code Online (Sandbox Code Playgroud)

Swift 4.0:

tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.none, animated: true)
Run Code Online (Sandbox Code Playgroud)

  • 如果最后一部分为空怎么办? (2认同)