我的 Storyboard 中有一个 UICollectionView
,其中有一个 UICollectionViewCell
,我已单击并拖动它以使其大小为 100w 100h。我也有一个 swift 文件控制这个。代码如下:
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//#warning Incomplete method implementation -- Return the number of sections
return 4
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//#warning Incomplete method implementation -- Return the number of items in the section
return 10
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! UICollectionViewCell
// Configure the cell
return cell
}
然而,当我运行它时,第一行被拉伸(stretch)成这样:
如何让它们的大小相同?
最佳答案
您的问题不是“拉伸(stretch)的”第一行单元格。它是 0 点的部分插入,这导致部分 0 的最后一个单元格与部分 1 的第一个单元格齐平。
您可以通过实现 UICollectionViewDelegateFlowLayout
并设置部分插图来解决此问题。让你的类符合UICollectionViewDelegateFlowLayout
,将collectionView.delegate
设置为self
,实现UICollectionViewDelegateFlowLayout
需要的功能。相关委托(delegate)功能包括:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat
关于ios - UICollectionView 显示多种尺寸的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30415106/