gpt4 book ai didi

swift - UICollectionViewLayout 方法永远不会被调用

转载 作者:搜寻专家 更新时间:2023-11-01 05:43:54 38 4
gpt4 key购买 nike

我正在使用 UICollectionView 并尝试使用 UICollectionViewLayout 方法来调整单元格的大小,但它们从未被调用过。这些方法是:

sizeForItemAtIndexPathminimumInterimItemSpacingForSectionAtIndexminimumLineSpacingForSectionAtIndex

这 3 个方法从未被调用,我不确定从哪里/如何调用它们。下面是我在 Swift 3 中的代码

import Foundation
import UIKit

class CollectionVC: UICollectionViewController {

var Sections = [SectionService]()

override func viewDidLoad() {
createServiceSections()
}

func createServiceSections(){

ServiceSections.append(ServiceSection(headerTitle: "1", items: 2, headerImage: UIImage(named: "Default"), stats: nil))
ServiceSections.append(ServiceSection(headerTitle: "2", items: 3, headerImage: UIImage(named: "Default"), stats: nil))
ServiceSections.append(ServiceSection(headerTitle: "3", items: 3, headerImage: UIImage(named: "Default"), stats: nil))
ServiceSections.append(ServiceSection(headerTitle: "4", items: 5, headerImage: UIImage(named: "Default"), stats: nil))
}

override func numberOfSections(in collectionView: UICollectionView) -> Int {
return ServiceSections.count
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return ServiceSections[indexPath.section].items.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "servicecell", for: indexPath) as! ServicesCellView

cell1.serviceLabel.text = "test"
cell1.serviceImage.image = ServiceSections[indexPath.section].headerImage
return cell1
}

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

print("size for item at indexPath")
var cellSize: CGSize
let collectionViewWidth = self.collectionView!.bounds.size.width
cellSize = CGSize(width: (collectionViewWidth * 0.48), height: (collectionViewWidth * 0.48))
return cellSize
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInterItemSpacingForSectionAtIndex section: Int) -> CGFloat {
print("min spacing section")
return 1
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
print("min line spacing section")
return 1
}
}

最佳答案

在 Xinatanil 的帮助下,我解决了这个问题。首先,我不得不将我的类(class)线改成这个

class CollectionVC: UICollectionViewController, UICollectionViewDelegateFlowLayout {

然后我不得不将方法声明更改为 Swift 3 语法...

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

var cellSize: CGSize
let collectionViewWidth = self.collectionView!.bounds.size.width
cellSize = CGSize(width: (collectionViewWidth * 0.48), height: (collectionViewWidth * 0.48))
return cellSize
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 1
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 1
}

关于swift - UICollectionViewLayout 方法永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40640260/

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