ios UITableView 最后一行的 indexPath
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31308113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITableView indexPath of last row
提问by Scott Kilbourn
I am attempting to make the last row in a UITableViewvisible, after it has been added. Right now, when I add a row and call reloadData, the table goes to the top.
添加后,我试图使最后一行UITableView可见。现在,当我添加一行并调用 reloadData 时,表格会移到顶部。
I figure if I get the indexPathfor the last row, that I can select that row and it should appear in the list. I am unsure of how to get that value, or even if I am approaching this correctly.
我想如果我得到indexPath最后一行的,我可以选择该行并且它应该出现在列表中。我不确定如何获得该值,或者即使我正确地接近这个值。
How do I get an indexPathfor a specific row?
如何获得indexPath特定行的 ?
回答by Shamsudheen TK
Please note that, you don't need to call the reloadDatato make the last row visible. You can make use of scrollToRowAtIndexPathmethod.
请注意,您不需要调用reloadData使最后一行可见。你可以使用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)
回答by Yair hadad
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)
}
}
回答by Mahendra
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
希望这会帮助其他人
回答by Vinod Supnekar
As suggested by others get indexPath for perticular sections like section 0.
正如其他人所建议的那样,为诸如第 0 节之类的特定部分获取 indexPath。
After that call...add this methos in cellFOrROwAtIndex
在那次通话之后......添加这个方法 cellFOrROwAtIndex
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];..to scroll to specific indexPath in TableView.
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];..滚动到 TableView 中的特定 indexPath。
Note:-But it still need scrolling of tableview in Downward direction.
注意:-但它仍然需要向下滚动 tableview。
回答by Kiran Jasvanee
Not mandatorily required to get the indexpath of last row.
You can set the CGPoint of UITableview to show a last row you added.
I always use this code in my chat application to show a last added message.
不是强制要求获取最后一行的索引路径。
您可以设置 UITableview 的 CGPoint 以显示您添加的最后一行。
我总是在我的聊天应用程序中使用此代码来显示最后添加的消息。
//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.
我希望它对你有用。
谢谢。
回答by Oxcug
You shouldn't be using -reloadDatafor this use case. What you're looking for is -insertRowsAtIndexPaths:withRowAnimation:.
您不应该-reloadData用于此用例。你要找的是-insertRowsAtIndexPaths:withRowAnimation:.
Feel free to ask if you want some usage examples or a more detailed explanation as to why using -reloadDatasend you to the top of the UITableView.
随意询问您是否需要一些使用示例或更详细的解释,以了解为什么使用-reloadData将您发送到UITableView.

