最后一行的UITableView indexPath

时间:2015-07-09 04:26:02

标签: ios swift uitableview

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

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

如何获取特定行的<div id="wrapper"> <div id="content"> <div class="menu"> <script> function changetitle_home() { document.getElementById("contenttitle").innerHTML = "Home"; } function changetitle_news() { document.getElementById("contenttitle").innerHTML = "News"; } function changetitle_contact() { document.getElementById("contenttitle").innerHTML = "Contact"; } function changetitle_about() { document.getElementById("contenttitle").innerHTML = "About Us"; } </script> <ul> <li><a href="#" onclick="changetitle_home()">Home</a></li> <li><a href="#" onclick="changetitle_news()">News</a></li> <li><a href="#" onclick="changetitle_contact()">Contact</a></li> <li><a href="#" onclick="changetitle_about()">About</a></li> </ul> </div> <div class="body_content"> <div class="paneltitle" id="contenttitle">Home</div> </div> </div>

7 个答案:

答案 0 :(得分:27)

请注意,您不需要致电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)

答案 1 :(得分:3)

您可以使用带扩展名的scrollToRowAtIndexPath:

在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)
    }
}

答案 2 :(得分:1)

正如其他人所建议的那样,获取indexPath作为第0节等特定部分。

在那次电话会议之后......在<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <!-- To show result in the dom instead of console, only to be used in the snippet not in production --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

中添加此方法

cellFOrROwAtIndex ..滚动到TableView中的特定indexPath。

注意: - 但仍然需要向下滚动tableview。

答案 3 :(得分:0)

您不应该在此用例中使用-reloadData。您正在寻找的是-insertRowsAtIndexPaths:withRowAnimation:

请随意询问您是否需要一些使用示例或更详细的解释,说明为何使用-reloadData将您发送到UITableView的顶部。

答案 4 :(得分:0)

不强制要求获取最后一行的索引路径 您可以设置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;
}

我希望它对你有用。
感谢。

答案 5 :(得分:0)

Shamsudheen TK的答案将崩溃

  

如果表格视图中没有行/部分。

以下避免运行时崩溃的解决方案

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)
  }
}

注意:如果您要滚动到块/样式的底部,则需要在主线程上调用它。

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

希望这对其他人有帮助

答案 6 :(得分:0)

Swift 5.0 +

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