Tamannah Tamannah - 1 month ago 10
iOS Question

why does the dynamic collection cells in collection view doesnt display as given size and changes after scrolling in swift 3

I have a collection view.I need to display cells with varying width at sequences. First 4 cells get displayed as required but after that consecutive 5th image disappears and others are displayed but not in the desired manner. But it doesnot show in the simulator. The view gets distorted when scrolling the collection view.Please anyone help me out in swift 3.

Answer Source

If you want to resize UICollectionViewCell try to implement this method, setting frame inside cellForRowAt is wrong.

func collectionView(_ collectionView: UICollectionView!, layout _: UICollectionViewLayout!, sizeForItemAtIndexPath indexPath: IndexPath!) -> CGSize {
    if indexPath.row%3 == 0 {
        return CGSize(width: 172, height: 120)
    } else {
        .....
    }//switch, if, whatever
}