如何在 ios 中聚焦 UITableview 中的最后一个单元格 [英] How to focus last cell in the UITableview in ios

查看:31
本文介绍了如何在 ios 中聚焦 UITableview 中的最后一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个聊天界面.

I am creating a chat interface.

用户的消息放在一个新的 UITable 视图单元格中.

User's message is put in a new UITable view cell.

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

And when update the table view, I use the following code.

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

实际上这样效果会好一点,但有一些奇怪的地方.

Actually this works a little better, but there is something strange.

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

When I turn off the app and turn it on again, or after exiting the screen and entering again, the following issues arise.

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

The issue is that when I add a new cell, it goes up to the first cell in the table view and back down to the last cell.

查看问题(https://vimeo.com/266821436)

随着单元格数量的增加,滚动变得太快太乱.

As the number of cells increases, the scrolling becomes too fast and too messy.

我只想不断更新新注册的最后一个单元格.

I just want to keep updating the last cell that is newly registered.

我该怎么办?

推荐答案

请使用 DispatchQueue 来滚动,因为你触发的方法是用 tableView 加载数据执行的,所以我们需要给滚动时间.

Please use DispatchQueue to Scroll because of the method you are fire is executed with tableView load data so we need to give time to scroll.

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

这篇关于如何在 ios 中聚焦 UITableview 中的最后一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆