I have table view with nested collection view(horizontal scrolling) into table view cell. Something like that:
-table view:
- section
- cell:
- collection view
On starting screen I have 3 sections with 1 cell in each section. When I try to reuse 1 section with cell, indexPath for focused item don't change.
For example, if I select 1 section 1 cell and focused 12th item in collection view, then I scroll down table view in this manner I change focused item and switch to another section of table view. So I reuse cell and see the same focused indexPath (12 row). So collection view scrolles to this item.
I tried to reset preferredFocusEnvironments in
override func prepareForReuse()
but it had no result. And when I set to false collectionView.remembersLastFocusedIndexPath = false
it didn't help me to solve the problem. How can I fix this problem?
I found out that problem was in contentOffset in collectionView. When I reused tableViewCell I didn't reset offset in collectionView. So I saved all offset for each collectionView in tableViewCell and set this value by key to collectionView. Something like this(in func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
):
return tableView.dequeueReusableCell(cellClass: HomeTableViewCell.self, for: indexPath).adjust {
if let id = $0.feedID {
contentOffsets[id] = $0.collectionView.contentOffset
}
$0.preprareForDisplay(feed: feeds[indexPath.section)
$0.collectionView.contentOffset = contentOffsets[feeds[indexPath.section].origin.id] ?? .zero
}
So after that focus work correct.