ios - UICollectionView 显示多种尺寸的单元格?

标签 ios swift uicollectionview uicollectionviewcell

我的 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)成这样:enter image description here

如何让它们的大小相同?

最佳答案

您的问题不是“拉伸(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/

相关文章:

ios - Swift3中如何根据内容自动改变UICollectionView Cell的高度?

ios - 我想自己实现 promise ,但找不到解决方案

ios - 如何在我的 UICollectionView 上重新加载数据?

ios - DidSelectItemAtIndexPath 不适用于 collectionview

ios - 文件 “XXX” 不存在

ios - Swift 中的 "func observeValueForKeyPath(keyPath : NSString, object : AnyObject, change : NSDictionary, context : Void)"问题

ios - 无法将本地 PDF 文件加载到 webview

iphone - 如何缓冲您的 iPhone 应用程序正在观察的传入事件/通知,以免触发每个事件的事件代码?

ios - 基于 Swift SpriteKit 分数更改背景

ios - 如何在 Objective-C 中为蒙版图像添加不带蒙版的边框