Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView is jumping when I insert new rows

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

and insert rows here:

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

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

like image 887
aaisataev Avatar asked Mar 14 '18 18:03

aaisataev


People also ask

Which method is used to allow moving of row in UITableView?

moveRow(at:to:) Moves the row at a specified location to a destination location.

What is Tableview?

A table view displays a single column of vertically scrolling content, divided into rows and sections. Each row of a table displays a single piece of information related to your app.


2 Answers

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.

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()
    }
}
like image 77
Mandeep Singh Avatar answered Oct 23 '22 13:10

Mandeep Singh


I had a similar problem with tableView. Partially I decided this with beginUpdates() and endUpdates()

self.tableView.beginUpdates()
self.tableView.endUpdates()

But this didn't solve the problem. For iOS 11, the problem remained. I added an array with the heights of all the cells and used this data in the method tableView(_:heightForRowAt:)

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return cellHeights[indexPath.row] ?? 0
}

Also add this method tableView(_:estimatedHeightForRowAt:)

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return cellHeights[indexPath.row] ?? 0
}

After that, the jumps stopped.

like image 23
maxwell Avatar answered Oct 23 '22 15:10

maxwell