iosobjective-cxcodeuicollectionviewuicollectionviewcell

UICollectionView Set number of columns


I just started learning about UICollectionViews. I'm wondering if anyone knows how to specify the number of columns in a collectionview. The default is set to 3 (iPhone/portrait). I've looked at the documentation and can't seem to find a concise answer.


Solution

  • CollectionViews are very powerful, and they come at a price. Lots, and lots of options. As omz said:

    there are multiple ways you could change the number of columns

    I'd suggest implementing the <UICollectionViewDelegateFlowLayout> Protocol, giving you access to the following methods in which you can have greater control over the layout of your UICollectionView, without the need for subclassing it:

    Also, implementing the following method will force your UICollectionView to update it's layout on an orientation change: (say you wanted to re-size the cells for landscape and make them stretch)

    -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                   duration:(NSTimeInterval)duration{
    
        [self.myCollectionView.collectionViewLayout invalidateLayout];
    }
    

    Additionally, here are 2 really good tutorials on UICollectionViews:

    http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12

    http://skeuo.com/uicollectionview-custom-layout-tutorial