当我插入新行时 UITableView 正在跳跃 [英] UITableView is jumping when I insert new rows

查看:35
本文介绍了当我插入新行时 UITableView 正在跳跃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了很多方法来解决这个问题,但我做不到.我的 tableView 在加载更多数据后跳转.我在willDisplay中调用下载方法:

I tried many ways to solve this problem, but I couldn't. My tableView jumps after it loads more data. I call the downloading method in willDisplay:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    let lastObject = objects.count - 1
    if indexPath.row == lastObject {
        page = page + 1
        getObjects(page: page)
    }
}

并在此处插入行:

func getObjects(page: Int) {
    RequestManager.sharedInstance.getObjects(page: page, success: { objects in
        DispatchQueue.main.async(execute: {
            self.objects = self.objects + objects
            self.tableView.beginUpdates()
            var indexPaths = [IndexPath]()
            for i in 0...objects.count - 1 {
                indexPaths.append(IndexPath(row: i, section: 0))
            }
            self.tableView.insertRows(at: indexPaths, with: .bottom)
            self.tableView.endUpdates()
        });
    })
}

那我做错了什么?为什么tableView插入新行后会跳转?

So what do I wrong? Why tableView jumps after inserting new rows?

推荐答案

我刚刚找到了停止跳表视图的解决方案,而在表视图中插入多行.我正面临这个问题最近几天终于解决了这个问题.

I have just find the solution to stop jumping the table view while inserting multiple rows in the table View. Am facing this issue from last few days so finally I resolved the issue.

我们只需要设置表格视图的内容偏移,而在表视图中插入行.你只需要传递你的数组IndexPath 其余的你可以使用这个函数.

We have to just set the content offset of table view while inserting rows in the table view. You have to just pass your array of IndexPath rest you can use this function.

希望这个方法能帮到你.

Hope so this method will help you.

 func insertRows() {
    if #available(iOS 11.0, *) {
        self.tableView.performBatchUpdates({
            self.tableView.setContentOffset(self.tableView.contentOffset, animated: false)
            self.tableView.insertRows(at: [IndexPath], with: .bottom)
        }, completion: nil)
    } else {
        // Fallback on earlier versions
        self.tableView.beginUpdates()
        self.tableView.setContentOffset(self.tableView.contentOffset, animated: false)
        self.tableView.insertRows(at: [IndexPath], with: .right)
        self.tableView.endUpdates()
    }
}

这篇关于当我插入新行时 UITableView 正在跳跃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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