gpt4 book ai didi

ios - 使用 UICollectionView 具有不同单元格大小的部分

转载 作者:搜寻专家 更新时间:2023-10-30 23:01:37 28 4
gpt4 key购买 nike

我必须在 Swift 项目中创建一个包含两个不同部分的 View 。第一个有七个正方形的单元格。第二个有三个长方形的单元格。单元格内的数据是静态的。在所有情况下,这些部分都应适合整个屏幕宽度(主要是横向模式)。

我没有太多使用 UICollectionViewController 的经验,所以我决定使用一个 UICollectionViewController 类和两个可重用单元格来完成此操作,但是当我设置第一部分单元格宽度时第二个也受到影响,然后单元格宽度被削减。

我的观点正确吗?我应该使用不同的 UICollectionViewController(每个部分一个)吗?

更新:我的 Controller 代码

import UIKit

class InverterController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

let moduleCell = "moduleCell"
let parameterCell = "parameterCell"

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
if section > 0 {
return CGSizeZero
} else {
return CGSize(width: collectionView.frame.width, height: CGFloat(195))
}
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtPath indexPath: NSIndexPath) -> CGSize {

var newSize = CGSizeZero

if indexPath.section == 0 {

newSize = CGSizeMake(CGFloat(105), CGFloat(105))
} else {
newSize = CGSizeMake(CGFloat(313), CGFloat(200))
}

return newSize
}

override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "inverterHeader", forIndexPath: indexPath)
return headerView
default:
assert(false, "Unexpected element kind")
}
}

/**
Two sections in this UICollectionView: First one for clock items, second one for other parameters.
*/
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 2
}

/**
First section contains one cell
Second section contains three cells
*/
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
var items = 0

if section == 0 {
items = 7
} else {
items = 3
}

return items
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

var cellIdentifier = ""

if indexPath.section == 0 {
cellIdentifier = moduleCell
} else {
cellIdentifier = parameterCell
}

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath)
cell.layer.cornerRadius = 6
cell.layer.masksToBounds = true

return cell
}
}

CollectionView 的 Interface Builder 配置 enter image description here

模拟器截图(非预期结果;所有单元格的宽度相同) enter image description here

最佳答案

您可以使用 collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize 委托(delegate)方法来调整单元格大小。

在你的情况下你可以这样写:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

let width : CGFloat
let height : CGFloat

if indexPath.section == 0 {
// First section
width = collectionView.frame.width/7
height = 50
return CGSizeMake(width, height)
} else {
// Second section
width = collectionView.frame.width/3
height = 50
return CGSizeMake(width, height)
}

}

将此代码放入您的 collectionViewController 类中。
height 设置为单元格的高度。如果您在 collectionView 中使用单元格或插图之间的间距,则可能需要调整 width

关于ios - 使用 UICollectionView 具有不同单元格大小的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39110644/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com