使用UITableView的scrollToRowAtIndexPath不起作用

Kob*_*zzo 22 uitableview swift xcode6 ios8

我在iOS8中有一个像这样的表格视图:

tableView = UITableView(frame: view.bounds, style: .Plain)
view.addSubview(tableView)
Run Code Online (Sandbox Code Playgroud)

当用户在键盘中键入并发送一些文本时,应用程序会调用以下方法来滚动tableView.目标是在屏幕上查看新文本(如聊天)

let numberOfRows = tableView.numberOfRowsInSection(0)
        if numberOfRows > 0 {
            let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: 0)
            tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: animated)
        }
Run Code Online (Sandbox Code Playgroud)

但表视图不会滚动到bootom.

有人有解决方案吗?

谢谢.

说明:

课程定义:

class ChatViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextViewDelegate 
Run Code Online (Sandbox Code Playgroud)

然后我有表视图的定义:

var tableView: UITableView! 
Run Code Online (Sandbox Code Playgroud)

在viewDidLoad中:

tableView = UITableView(frame: view.bounds, style: .Plain) 
tableView.dataSource = self 
tableView.delegate = self 
view.addSubview(tableView) 
Run Code Online (Sandbox Code Playgroud)

然后我调用了应该进行滚动的代码(我的答案中的第二个代码块).当我启动应用程序时,我希望tableview向下滚动,但它不起作用.

Fox*_*150 80

下面的解决方案对我有用.它是StackOverflow上的解决方案组合.scrollToRowAtIndexPath经过很短的延迟,我打电话给" ".

func tableViewScrollToBottom(animated: Bool) {

    let delay = 0.1 * Double(NSEC_PER_SEC)
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

    dispatch_after(time, dispatch_get_main_queue(), {

        let numberOfSections = self.tableView.numberOfSections()
        let numberOfRows = self.tableView.numberOfRowsInSection(numberOfSections-1)

        if numberOfRows > 0 {
            let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1))
            self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: animated)
        }

    })
}
Run Code Online (Sandbox Code Playgroud)

叫做:

tableViewScrollToBottom(true)
Run Code Online (Sandbox Code Playgroud)

斯威夫特3

func tableViewScrollToBottom(animated: Bool) {
    DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(300)) {
        let numberOfSections = self.tableView.numberOfSections
        let numberOfRows = self.tableView.numberOfRows(inSection: numberOfSections-1)

        if numberOfRows > 0 {
            let indexPath = IndexPath(row: numberOfRows-1, section: (numberOfSections-1))
            self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: animated)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这是我到目前为止找到的最佳解决方案.我不得不将延迟增加到0.5,否则有时会在tableView.reloadData完成之前调用它.我尝试了SO上的每个解决方案(子类`TableViewDelegate`并覆盖`reloadData`;在主线程上执行`dispatch_asynch`等)以滚动到底部,它们似乎都不再适用于**iOS 9**. (2认同)

Kob*_*zzo 9

我的问题的解决方案是:

let numberOfSections = tableView.numberOfSections()
let numberOfRows = tableView.numberOfRowsInSection(numberOfSections-1)

if numberOfRows > 0 {
    println(numberOfSections)
    let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1))
    tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: animated)
}
Run Code Online (Sandbox Code Playgroud)


Ank*_*ain 6

在调用scroll之前先尝试重新加载.很受欢迎,但适用于iOS8.

   tableView.reloadData()
   tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: animated)
Run Code Online (Sandbox Code Playgroud)


Phi*_*hil 6

@ Fox5150的SWIFT 3版本答案,扩展名为:

extension UITableView {

    func tableViewScrollToBottom(animated: Bool) {

        DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {

            let numberOfSections = self.numberOfSections
            let numberOfRows = self.numberOfRows(inSection: numberOfSections-1)
            if numberOfRows > 0 {
                let indexPath = IndexPath(row: numberOfRows-1, section: (numberOfSections-1))
                self.scrollToRow(at: indexPath, at: UITableViewScrollPosition.bottom, animated: animated)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

self.tableView.tableViewScrollToBottom(animated: true)
Run Code Online (Sandbox Code Playgroud)


flu*_*nic 0

如果键盘仍然可见,您必须设置 的UITableView属性contentInset来补偿屏幕底部的键盘高度。否则,您的单元格可能隐藏在键盘后面,因为它UITableView不知道其内容的一半被它隐藏了。

或者使用UITableViewController自动处理的。