gpt4 book ai didi

swift - UICollectionViewDelegateFlowLayout 方法未在 Swift 2.3 App 中调用

转载 作者:搜寻专家 更新时间:2023-10-30 21:55:04 24 4
gpt4 key购买 nike

我的 UICollectionViewDelegateFlowLayout 方法都没有被调用。有人可以帮我找到解决方案吗?

在我的 ViewDidLoad 方法中,我已将 Collection View 的委托(delegate)和数据源设置为self.collectionView.delegate = selfself.collectionView.dataSource = self

这是我的委托(delegate)方法:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
let deviceIdiom = UIScreen.mainScreen().traitCollection.userInterfaceIdiom
switch(deviceIdiom){
case .Phone:

return UIEdgeInsets(top: 50, left: 10, bottom: 0, right: 10)
case .Pad:
return UIEdgeInsets(top: 60, left: 20, bottom: 0, right: 20)
default:
break
}
}

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

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

let deviceIdiom = UIScreen.mainScreen().traitCollection.userInterfaceIdiom

switch(deviceIdiom){
case .Phone:

let cellWidth = (view.bounds.size.width - 16) / 2
let cellHeight = cellWidth
return CGSize(width: cellWidth, height: cellHeight)

case .Pad:

let cellWidth = (view.bounds.size.width - 40) / 2
let cellHeight = cellWidth
return CGSize(width: cellWidth, height: cellHeight)

default:
break
}
}

我的 ViewDidLoad 方法-

覆盖函数 viewDidLoad() { super.viewDidLoad()

 self.collectionView.dataSource = self
self.collectionView.delegate = self
cellImages = [
"contact.png",
"share.png",
"setting.png",
"logout.png"]
cellLabels = [
"Contact",
"Share",
"Setting",
"Logout"]

self.view.backgroundColor = uicolorFromHex(0xE5DADA)

self.collectionView?.backgroundColor = uicolorFromHex(0xE5DADA)
self.navigationController?.navigationBarHidden = false
self.navigationItem.hidesBackButton = true
navigationController!.navigationBar.titleTextAttributes =
[NSForegroundColorAttributeName: UIColor.whiteColor()]
navigationController!.navigationBar.barTintColor = UIColor(red: 0.0431, green: 0.0824, blue: 0.4392, alpha: 1.0)

if let user = User.currentUser(){
if(user.manager){
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Board", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(goToBoard))

}

谢谢,

最佳答案

我终于通过创建类的扩展解决了这个问题 MainViewController : UICollectionViewDelegateFlowLayout { } ,将所有委托(delegate)方法放在那里并清理构建。它在 Swift 3(在同一个类中)中工作,但对于 swift 2.3 出于某种奇怪的原因,它需要扩展。谢谢,

关于swift - UICollectionViewDelegateFlowLayout 方法未在 Swift 2.3 App 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41920337/

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