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>
协议,让您可以访问以下方法,您可以在其中更好地控制 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: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屋!