iOS UICollectionView prototype cell size property ignored

后端 未结 9 1939
花落未央
花落未央 2021-01-30 19:51

I\'m using a UICollectionView with two prototype cells. The prototype cells have different widths and contain different controls (image view and web view). I\'m definitely ret

相关标签:
9条回答
  • 2021-01-30 20:28

    In Swift you can call sizeForItemAtIndexPath delegate method to set the size for each cell.

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    
            if indexPath.row == 0 {
                    // Set the size for cell no. 0
             }else if indexPath.row == 1 {
                   // Set the size for cell no. 1
            }
        }
    

    I hope that helps. Thanks!

    0 讨论(0)
  • 2021-01-30 20:31

    I had a Similar Issue, I set a breakpoint on the sizeForItemAt and it was being called for every cell I had on my collection, however, the size set in code was never being reflected on the device.

    While looking at other collections in my project I noticed the Collection View Flow Layout was slightly different in the collection with the issue.

    By Selecting the Flow Layout in the Storyboard

    And then Setting the Cell Size & Estimate Size in the Size Inspector

    This helped me solve my issue.

    However the size of the cell will not be dynamic with what is in your sizeForItemAt

    0 讨论(0)
  • 2021-01-30 20:31

    If all your cells are the same size, you should simply set the UICollectionView's Cell size properties to the same as the UICollectionViewCell's size properties.

    0 讨论(0)
提交回复
热议问题