UICollectionView设置列数 [英] UICollectionView Set number of columns
问题描述
我刚开始学习UICollectionViews。我想知道是否有人知道如何指定集合视图中的列数。默认设置为3(iPhone /肖像)。我查看了文档,似乎无法找到简明的答案。
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.
推荐答案
CollectionViews功能非常强大,而且它们非常强大付出代价。很多,有很多选择。正如omz所说:
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
我建议实施 < UICollectionViewDelegateFlowLayout>
协议,通过以下方法,您可以更好地控制<$的布局c $ c> UICollectionView ,无需对其进行子类化:
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:
-
collectionView: layout:insetForSectionAtIndex:
-
collectionView:layout:minimumInteritemSpacingForSectionAtIndex:
-
collectionView:layout:minimumLineSpacingForSectionAtIndex:
-
collectionView:layout:referenceSizeForFooterInSection:
-
collectionView:layout:referenceSizeForHea derInSection:
-
collectionView:layout:sizeForItemAtIndexPath:
collectionView:layout:insetForSectionAtIndex:
collectionView:layout:minimumInteritemSpacingForSectionAtIndex:
collectionView:layout:minimumLineSpacingForSectionAtIndex:
collectionView:layout:referenceSizeForFooterInSection:
collectionView:layout:referenceSizeForHeaderInSection:
collectionView:layout:sizeForItemAtIndexPath:
此外,实现以下方法将强制您的UICollectionView在方向更改时更新它的布局:(假设您想为景观重新调整单元格大小并使它们伸展)
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];
}
此外,这里有2个非常好的教程 UICollectionViews
:
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
这篇关于UICollectionView设置列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!