简体   繁体   English

UITableView 最后一行的 indexPath

[英]UITableView indexPath of last row

I am attempting to make the last row in a UITableView visible, after it has been added.在添加UITableView后,我试图使其中的最后一行可见。 Right now, when I add a row and call reloadData, the table goes to the top.现在,当我添加一行并调用 reloadData 时,表格会移到顶部。

I figure if I get the indexPath for the last row, that I can select that row and it should appear in the list.我想如果我得到最后一行的indexPath ,我可以选择该行并且它应该出现在列表中。 I am unsure of how to get that value, or even if I am approaching this correctly.我不确定如何获得该值,或者即使我正确地接近这个值。

How do I get an indexPath for a specific row?如何获取特定行的indexPath

Please note that, you don't need to call the reloadData to make the last row visible.请注意,您不需要调用reloadData来使最后一行可见。 You can make use of scrollToRowAtIndexPath method.您可以使用scrollToRowAtIndexPath方法。

You can use the below code to achieve your goal.您可以使用以下代码来实现您的目标。

// 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:斯威夫特 4.0:

tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.none, animated: true)

You can use scrollToRowAtIndexPath with extension:您可以使用带有扩展名的 scrollToRowAtIndexPath:

In Swift 3:在 Swift 3 中:

extension UITableView {
    func scrollToLastCell(animated : Bool) {
        let lastSectionIndex = self.numberOfSections - 1 // last section
        let lastRowIndex = self.numberOfRows(inSection: lastSectionIndex) - 1 // last row
        self.scrollToRow(at: IndexPath(row: lastRowIndex, section: lastSectionIndex), at: .Bottom, animated: animated)
    }
}

Shamsudheen TK's answer will crash Shamsudheen TK 的回答会崩溃

if there is no rows/sections in tableview.如果 tableview 中没有行/节。

The following solution to avoid crash at run time以下解决方案避免在运行时崩溃

extension UITableView {
  func scrollToBottom() {

    let lastSectionIndex = self.numberOfSections - 1
    if lastSectionIndex < 0 { //if invalid section
        return
    }

    let lastRowIndex = self.numberOfRows(inSection: lastSectionIndex) - 1
    if lastRowIndex < 0 { //if invalid row
        return
    }

    let pathToLastRow = IndexPath(row: lastRowIndex, section: lastSectionIndex)
    self.scrollToRow(at: pathToLastRow, at: .bottom, animated: true)
  }
}

Note: If you are trying to scroll to bottom in block/clousure then you need to call this on main thread.注意:如果您试图在块/闭包中滚动到底部,那么您需要在主线程上调用它。

DispatchQueue.main.async {
  self.tableView.scrollToBottom()
}

Hope this will helps other希望这会帮助其他人

As suggested by others get indexPath for perticular sections like section 0.正如其他人所建议的那样,为诸如第 0 节之类的特定部分获取 indexPath。

After that call...add this methos in cellFOrROwAtIndex在那次调用之后……在cellFOrROwAtIndex添加这个cellFOrROwAtIndex

[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; ..to scroll to specific indexPath in TableView. ..滚动到 TableView 中的特定 indexPath。

Note:-But it still need scrolling of tableview in Downward direction.注意:-但它仍然需要向下滚动 tableview。

You shouldn't be using -reloadData for this use case.您不应该在此用例中使用-reloadData What you're looking for is -insertRowsAtIndexPaths:withRowAnimation: .您正在寻找的是-insertRowsAtIndexPaths:withRowAnimation:

Feel free to ask if you want some usage examples or a more detailed explanation as to why using -reloadData send you to the top of the UITableView .随意询问您是否需要一些使用示例或更详细的解释,说明为什么使用-reloadData会将您发送到UITableView的顶部。

Not mandatorily required to get the indexpath of last row.不是强制要求获取最后一行的索引路径。
You can set the CGPoint of UITableview to show a last row you added.您可以设置 UITableview 的 CGPoint 以显示您添加的最后一行。
I always use this code in my chat application to show a last added message.我总是在我的聊天应用程序中使用此代码来显示最后添加的消息。

//Declaration
@IBOutlet weak var tableview: UITableView!

//Add this executable code after you add this message.
var tblframe: CGRect = tableview.frame
tblframe.size.height = self.view.frame.origin.y
tableview.frame = tblframe
var bottomoffset: CGPoint = CGPointMake(0, tableview.contentSize.height - tableview.bounds.size.height)
if bottomoffset.y > 0 {
    tableview.contentOffset = bottomoffset;
}

I hope it will work for you.我希望它对你有用。
Thanks.谢谢。

Swift 5.0 +斯威夫特 5.0 +

 extension UITableView {
    func isLastVisibleCell(at indexPath: IndexPath) -> Bool {
        guard let lastIndexPath = indexPathsForVisibleRows?.last else {
            return false
        }
        return lastIndexPath == indexPath
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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