获取 CollectionView 中的列数 IOS
Get number of column in a CollectionView IOS
我在我的第一个应用程序中使用 UICollectionView
。我的问题是我无法获得 UICollectionView
中计算的列(或行)数。
UICollectionView
在 UIScrollView
内,我想设置 UICollectionView
的高度,因为我不想在 UICollectionView
中滚动,但是我想查看所有项目。
这可能吗?
谢谢,
F.G
经过多次研究,我找到了解决方案:
- 如果我想固定高度,我不需要知道集合视图中的列数或行数。
- 集合视图的高度可以自动更新:
向集合视图添加高度限制
在 ViewController 中添加 CollectionView 和约束作为出口:
@IBOutlet weak var heightConstrainCollectionView: NSLayoutConstraint!
@IBOutlet weak var myCollectionView: UICollectionView!
视图加载后,更新CollectionView的高度:
override func viewDidAppear(animated: Bool) {
super.viewDidLoad()
heightConstrainCollectionView.constant = myCollectionView.contentSize.height
}
如果用户旋转设备,更新 CollectionView 的高度:
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
heightConstrainCollectionView.constant = myCollectionView.contentSize.height
// Add this if you want to see the height after rotation
println("New Width: \(newspaperCollectionView.contentSize.height)")
}
现在我无法在 CollectionView 中滚动,而 CollectionView 显示所有项目
我在我的第一个应用程序中使用 UICollectionView
。我的问题是我无法获得 UICollectionView
中计算的列(或行)数。
UICollectionView
在 UIScrollView
内,我想设置 UICollectionView
的高度,因为我不想在 UICollectionView
中滚动,但是我想查看所有项目。
这可能吗?
谢谢, F.G
经过多次研究,我找到了解决方案:
- 如果我想固定高度,我不需要知道集合视图中的列数或行数。
- 集合视图的高度可以自动更新:
向集合视图添加高度限制
在 ViewController 中添加 CollectionView 和约束作为出口:
@IBOutlet weak var heightConstrainCollectionView: NSLayoutConstraint!
@IBOutlet weak var myCollectionView: UICollectionView!
视图加载后,更新CollectionView的高度:
override func viewDidAppear(animated: Bool) {
super.viewDidLoad()
heightConstrainCollectionView.constant = myCollectionView.contentSize.height
}
如果用户旋转设备,更新 CollectionView 的高度:
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
heightConstrainCollectionView.constant = myCollectionView.contentSize.height
// Add this if you want to see the height after rotation
println("New Width: \(newspaperCollectionView.contentSize.height)")
}
现在我无法在 CollectionView 中滚动,而 CollectionView 显示所有项目