Search code examples
iosswiftuitableviewuicollectionviewuicollectionviewcell

UITableView - reloadRows method creating weired scrolling bug


I am using UITableView in one of my app. inside tableview's row I have horizontal scrolling UICollectionView. Whenever user double tap on the collection view's cell it sends event back to the viewcontroller to update the datasource. After updation of datasource I am reloading that particular row using : tableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .none) But I am getting weird scrolling bug.

I wanted to reload the tableview's row and it's content i.e collectionview's content without any animation or scrolling issue.

I tried following solution to avoid this but no luck :(

UIView.performWithoutAnimation {
    self.tableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .none)
}

I even tried to remember content offset of the tableview and collectionview but that flicker is still there. Please guide me if you have come across such problem


Solution

  • That's a UI layout issue. When you reload a cell/section, it re-calculates its content size, hence your tableView content size specially if you have a dynamically sized cell. I would suggest 2 solutions

    Solution I (Hacky):

    • save the current view before-reload-state content offset of the table view and then scroll to it after the reload process with no animation

    Solution II (Better):

    • Update you data sources and their bonded cells directly without reloading your UITableView (consider maybe updating your UICollectionView instead - unless it's dynamically heisted you will need animate your UITableViewCell height).