如何在iOS中的uitable视图中焦点



我正在创建聊天接口。

用户的消息放在新的视图单元格中。

更新表视图时,我使用以下代码。

extension UITableView {
    func scrollToBottom() {
        let rows = self.numberOfRows(inSection: 0)
        if rows > 0 {
            let indexPath = IndexPath(row: rows - 1, section: 0)
            self.scrollToRow(at: indexPath, at: .bottom, animated: true)
        }
    }
}

实际上,这有点好一些,但是有一些奇怪的东西。

当我关闭应用程序并再次打开应用程序时,或在退出屏幕并再次输入后,会出现以下问题。

问题是,当我添加一个新单元格时,它会升至表视图中的第一个单元格,然后回到最后一个单元格。

请参阅问题(https://vimeo.com/266821436(

随着单元格的数量增加,滚动变得太快且太混乱了。

我只想继续更新新注册的最后一个单元格。

我该怎么办?

请使用dispatchqueue滚动,因为您是使用tableView加载数据的方法,因此我们需要给我们时间滚动。

extension UITableView {
    func scrollToBottom() {
        let rows = self.numberOfRows(inSection: 0)
        if rows > 0 {
            DispatchQueue.main.async {
                let indexPath = IndexPath(row: rows - 1, section: 0)
                self.scrollToRow(at: indexPath, at: .bottom, animated: true)
            }
        }
    }
}

func scrollToBottom(){
    DispatchQueue.main.async {
        let indexPath = IndexPath(row: self.array.count-1, section: 0)
        self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
    }
}

self.scrolltorow(at:indexpath.last ...(

最新更新