Eironeia
Eironeia

Reputation: 781

Scroll to last element of TableView take too much time

I'm trying to simulate a Whatsapp Chat any cell will have an image (for tail of the bubble), a bubble which is just View with color and some corner radius and a label which will represent the text of the message.

I've put a print before and after the call

self.messagesTableView.reloadData()

Once the after print is called tableView keeps some time doint I don't know what till the data is shown. And same happens with Insert row at indexpath, it takes some time till show the insert animation.

func displayMessages(viewModel: GroupChatMessages.GetChatMessages.ViewModel) {
    let displayedMessage = viewModel.displayedMessages

    print ("i'm here!")

    messages = displayedMessage!

    //self.messagesTableView.performSelectorOnMainThread(Selector("reloadData"), withObject: nil, waitUntilDone: true)

    self.messagesTableView.reloadData()

    print ("i'm here2!")

    firstTime = false
    self.setVisible(hiddenTableView: false, hiddenChatLoader: true)
    self.scrollToLastMessage(false)
    self.messagesLoaded = true

}

I've tried to do dispatched with queue, and the commented line before reloadData(), but nothings works and nothing represent a significative time.

Maybe could be for the image of the bubble? I don't know. I have this image saved on Assets, so I'm not downloading it from internet.

self.setVisible just hide the loader and show the tableView but I've tried too moving it up and nothings changes. Any further information you need let me know. Thanks!

EDIT:

Well I've seen that the problem comes from the scroll to last cell, this is where it takes the major part of the time.

 func scrollToLastMessage(animated: Bool) {
    let section = 0
    let lastItemIndex = self.messagesTableView.numberOfRowsInSection(section) - 1
    let indexPath:NSIndexPath = NSIndexPath.init(forItem: lastItemIndex, inSection: section)
    self.messagesTableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: animated)
    self.scrollDownButton.hidden = true
}

There is a posibility to optimize that scroll, because I have to do a Scroll because once the data is loaded, the first I've see is the top row of the tableView, but I would like to see the bottom one (last). Thanks!

Upvotes: 1

Views: 623

Answers (2)

Eironeia
Eironeia

Reputation: 781

Finally the solution that I've found to avoid dying while waiting scrolling to last element any single time, is swapping orientation of table

tableView.transform = CGAffineTransformMakeRotation(-M_PI); cell.transform = CGAffineTransformMakeRotation(M_PI);

Now headerView and footerView are reversed. For exemple, if you would like insert rows at (visually) at the bottom of the TableView with this configuration you should add it at position 0 forRow: 0 atSection: "WhereYouAre". This way when you add new element, no scroll is needed, because scroll is automatically. Amazing and strange answer IMHO.

I've found this solution here:

Solution Link

@Christos Hadjikyriacou solved there.

Upvotes: 1

Mousavian
Mousavian

Reputation: 1475

methods like reloadData() should be considered as UI methods and it's mandatory to call them in main thread:

DispatchQueue.main.async { tableView.reloadData() }

It's better not to use reloadData() function unless a significant amount of cells need to refresh or data source has been changed instead use this method to add new rows:

tableView.insertRows(at: [IndexPath], with: UITableViewRowAnimation)

and for refreshing cell:

tableView.reloadRows(at: [IndexPath], with: UITableViewRowAnimation)

also if the cell has a considerable amount of images and rendering, use this code to make scrolling faster:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
    // ADD THESE TWO LINE
    cell.layer.shouldRasterize = true
    cell.layer.rasterizationScale = UIScreen.main.scale
}

Using these ways will boost loading speed significantly

Upvotes: 1

Related Questions