2

i use collectionView with compositional layout and diffableDataSource collectionview cells are self sizing.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    currentSection = indexPath.row
    if let attributes = allCollectionView.collectionViewLayout.layoutAttributesForSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, at: IndexPath(item: 0, section: currentSection)) {
        let bottomOffset = CGPoint(x: 0, y: allCollectionView.bottomOffsetY)
        let offset = CGPoint(x: 0, y: attributes.frame.origin.y)
        chipCollectionView.selectItem(at: indexPath, animated: true, scrollPosition: .left)
        if attributes.frame.origin.y > bottomOffset.y {
            allCollectionView.setContentOffset(bottomOffset, animated: false)
        } else {
            allCollectionView.setContentOffset(offset, animated: false)
        }
    }
}

i use this code and The issue goes away once I begin scrolling around and trigger cell reuse/recomputation of layout (I suppose).

In this code, the position when setContentoffSet is executed is showing the row lists rather than the section above, and setContentOffset cannot find the correct position until the cell is reused, perhaps due to the effect of a dynamic cell. Has anyone experienced this problem?

ga-yo
  • 123
  • 1
  • 2
  • 11
  • I’m trying to maintain scroll position while adding items/sections. So far playing with shouldInvalidateLayout(forPreferredLayoutAttributes: withOriginalAttributes:) and the related invalidationContext function helps with a similar problem. A little finicky though – Shawn Throop Oct 26 '22 at 23:43

0 Answers0