2

After applying a search filter, we want the TableView to scroll to the top of the results. We have a TableView Header that scrolls with the content. So, top means that we want to see the TableView Header along with the first few cells. I found the following and tried a few solutions it had for iOS 10 and iOS 11: UITableView - scroll to the top However, we kept hitting bugs.

For example, we tried applying a scrollRectToVisible solution such as:

self.tableView.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)

However after applying the filter three times, the following gray area sometimes appeared above the TableView Header:

gray area

The scrollToView for the TableView Header solution in Programmatically scroll a UIScrollView to the top of a child UIView (subview) in Swift has the same gray area effect.

One other approach also produced the gray area:

UIView.animate(withDuration: 0.25) {
    self.tableView.setContentOffset(CGPoint.zero, animated: false)
}

So, we tried setContentOffset solutions such as this one:

if #available(iOS 11.0, *) {
    tableView.beginUpdates()
    tableView.setContentOffset(CGPoint(x: 0, y: -tableView.adjustedContentInset.top), animated: true)
    tableView.endUpdates()

} else {
    tableView.beginUpdates()
    tableView.setContentOffset(CGPoint(x: 0, y: -tableView.contentInset.top), animated: true)
    tableView.endUpdates()
}

However, it seems that there is a scenario where it won't take the user all the way to the top such as here: https://youtu.be/OmWCk5PedJE I am not positive, but perhaps it has to do with the cell being under the top a little.

It should be noted that the TableView is a Grouped style so that the HeaderView will scroll up and away. We're using a large estimatedRowHeight (huge number of 800) and rowHeight is set to UITableViewAutomaticDimension.

The document outline in Interface Builder is:

document outline

Any idea of a solution that takes one to the top of the search results without having the side effect of the gray area above the TableView Header view or having it fail if the cell is scrolled to a certain point first? Thank you.

finneycanhelp
  • 9,018
  • 12
  • 53
  • 77

1 Answers1

1

The fix was to have an explicit tableView(_:heightForRowAt:) We also deleted the assignment of the TableView's estimatedRowHeight and rowHeight.

So we added this code:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    return UITableViewAutomaticDimension
}

and used this code from another StackOverflow post to scroll to the top:

    if #available(iOS 11.0, *) {
        tableView.beginUpdates()
        tableView.setContentOffset(CGPoint(x: 0, y: -tableView.adjustedContentInset.top), animated: true)
        tableView.endUpdates()

    } else {
        tableView.beginUpdates()
        tableView.setContentOffset(CGPoint(x: 0, y: -tableView.contentInset.top), animated: true)
        tableView.endUpdates()
    }

and deleted this code:

    self.tableView.estimatedRowHeight = 800
    self.tableView.rowHeight = UITableViewAutomaticDimension

Believe it or not, the deletion of self.tableView.estimatedRowHeight = 800 was key to having things work. If we left that in there, it seemed like the setContentOffset would not take effect. The same goes for adding the beginUpdates and endUpdates.

finneycanhelp
  • 9,018
  • 12
  • 53
  • 77