jeudi 30 juin 2016

UITableView scroll to the bottom messed up with EstimatedRowHeight

I'm trying to scroll to the bottom of my UITableView (commentsFeed) whenever the user creates a new comment or the user refreshes the UITableView.

The code I use is:

func scrollToBottomOfComments() {

    var lastRowNumber = commentsFeed.numberOfRowsInSection(0) - 1
    var indexPath = NSIndexPath(forRow: lastRowNumber, inSection: 0)
    commentsFeed.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)
}

The problem is here in viewDidLoad:

commentsFeed.rowHeight = UITableViewAutomaticDimension
commentsFeed.estimatedRowHeight = 150

This basically states that the comments can have dynamic heights because users could post either really long comments or really short comments.

When I use the estimatedRowHeight, my scrollToBottom doesn't properly scroll to the bottom because it basically assumes my table height is commentsFeed.count * commentsFeed.estimatedRowHeight

This isn't correct though.

When I remove the estimatedRowHeight though, it doesn't seem to work either, and I think the reason is because it doesn't have the row height calculated properly because the rows each have dynamic heights.

How do I mitigate this?

Edit: It should be stated that the scroll doesn't end up at the right position, but the moment I use my finger to scroll anywhere, then the data jumps into place where it should have been via the scroll

Aucun commentaire:

Enregistrer un commentaire