0

I'm creating a list by UICollectionView with auto layout, I followed this answer and it worked fine until I add an expandable behavior.

enter image description here

I made a very simple project as you can see the screenshot above, all cells (red cells) are clickable, each cell contains a vertical stack view with two labels, title and description, the description label is hidden by default.

When a cell is selected, the description's hidden value will be changed, then I will call reloadItems(at:) to expand/fold the selected cell.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if let cell = collectionView.cellForItem(at: indexPath) as? DynamicCollectionViewCell {
        let dm = dataModels[indexPath.section][indexPath.item]
        dm.isExpanded = !dm.isExpanded
        
        cell.descLabel.isHidden = !dm.isExpanded
        cell.indicatorLabel.text = dm.isExpanded ? "-" : "+"
        collectionView.reloadItems(at: [indexPath])
    }
}

enter image description here

Suddenly the second header's position is moved! I've tried to use reloadData instead but the contentSet will be reset to 0. Why this could happen and how can I fix it?

Terry
  • 78
  • 9

0 Answers0