diff --git a/View Controllers/TableViewController.swift b/View Controllers/TableViewController.swift index 779e876..fa9c1d4 100644 --- a/View Controllers/TableViewController.swift +++ b/View Controllers/TableViewController.swift @@ -321,7 +321,9 @@ private extension TableViewController { } .store(in: &cancellables) - viewModel.updates.sink { [weak self] in self?.update($0) }.store(in: &cancellables) + viewModel.updates.receive(on: DispatchQueue.main) + .sink { [weak self] in self?.update($0) } + .store(in: &cancellables) viewModel.events.receive(on: DispatchQueue.main) .sink { [weak self] in self?.handle(event: $0) } @@ -370,15 +372,23 @@ private extension TableViewController { func update(_ update: CollectionUpdate) { let positionMaintenanceOffset: CGFloat + let preUpdateContentOffsetY = tableView.contentOffset.y + var setPreviousOffset = false if let itemId = update.maintainScrollPositionItemId, let indexPath = dataSource.indexPath(itemId: itemId) { positionMaintenanceOffset = tableView.rectForRow(at: indexPath).origin.y - - tableView.safeAreaInsets.top - tableView.contentOffset.y + - tableView.safeAreaInsets.top - preUpdateContentOffsetY } else { positionMaintenanceOffset = 0 } + if let headerView = tableView.tableHeaderView, + let headerViewWindowFrame = view.window?.convert(headerView.frame, from: headerView), + headerViewWindowFrame.maxY > 0 { + setPreviousOffset = true + } + self.dataSource.apply(update.sections.snapshot(), animatingDifferences: false) { [weak self] in guard let self = self else { return } @@ -395,6 +405,8 @@ private extension TableViewController { self.tableView.scrollToRow(at: indexPath, at: .top, animated: false) self.tableView.contentOffset.y -= positionMaintenanceOffset + } else if setPreviousOffset { + self.tableView.contentOffset.y = preUpdateContentOffsetY } } }