gpt4 book ai didi

ios - Swift - 在没有 Storyboard initWithFrame 错误的情况下创建 collectionView

转载 作者:行者123 更新时间:2023-11-28 10:40:05 25 4
gpt4 key购买 nike

我正在创建一个没有 Storyboard的 collectionView - 从我的项目中删除了它。

我在这里创建它,在 AppDelegate 中。我的页脚和页眉已正确出列,在加载图像时我显示的单元格为零,但我无法弄清楚调用此方法 initWithFrame 的位置!任何帮助表示赞赏。

不使用 Storyboard

在应用委托(delegate)中调用

let window = UIWindow(frame: UIScreen.main.bounds)
let myTabBar = TabBarVC()
window.rootViewController = myTabBar
window.makeKeyAndVisible()
let homeVC = HomeVC(collectionViewLayout: UICollectionViewFlowLayout())
myTabBar.present(homeVC, animated: false)

这最终失败并出现以下错误:

2018-06-26 16:02:15.379473-0700 MMDH[62033:2946934] -[MMDH.HomeVC 
initWithFrame:]: unrecognized selector sent to instance 0x7fdcce82f400
2018-06-26 16:02:15.389976-0700 MMDH[62033:2946934] *** Terminating app due to
uncaught exception 'NSInvalidArgumentException', reason: '-[MMDH.HomeVC
initWithFrame:]: unrecognized selector sent to instance 0x7fdcce82f400

我的 Cell、Footer、Header 都已出队。我不知道这是哪里出错了...继承人设置代码:

class HomeVC: UICollectionViewController, UICollectionViewDelegateFlowLayout, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var headerVC = HeaderView()
var footerVC = homeFooterView()

var photos = [Photo]()
var imageFetchOperationQueue = OperationQueue()
var infoOperationQueue = OperationQueue()

let width = UIScreen.main.bounds.width
let height = UIScreen.main.bounds.height
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()

let cache = NSCache<NSString, UIImage>()

override func viewDidLoad() {
super.viewDidLoad()

layout.itemSize = CGSize(width: (width / 3), height: (width / 3))
layout.sectionInset = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
layout.headerReferenceSize = CGSize(width: width, height: width / 2)
layout.minimumInteritemSpacing = 1
layout.minimumLineSpacing = 1

if photos.count == 0 {
layout.footerReferenceSize = CGSize(width: width, height: width)
} else if photos.count < 9 {
layout.footerReferenceSize = CGSize(width: 0, height: 0)
} else {
layout.footerReferenceSize = CGSize(width: 0, height: 0)
}
collectionView!.collectionViewLayout = layout

collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView?.dataSource = self
collectionView?.delegate = self

collectionView?.register(HomeVC.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "homeVC")
self.view.addSubview(headerVC)
collectionView?.register(homeFooterView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "footerVC")
self.view.addSubview(footerVC)
collectionView?.register(HomePicCell.self, forCellWithReuseIdentifier: "Cell")


}
}

最佳答案

您的 viewDidLoad 方法存在一些问题。

  1. 您的主要问题是将您的 HomeVC 类注册为页眉 View 类型。你想使用 HeaderView.self,而不是 HomeVC.self
  2. 不要根据屏幕尺寸进行布局。您的收藏 View 可能不会占据整个屏幕。布局基于 Collection View 的大小。另请注意,尺寸可能会发生变化。最好在 viewWillTransition 方法中更新布局。
  3. 这是一个 UICollectionViewController。您不应该创建自己的 UICollectionView。它会在调用 viewDidLoad 之前为您完成。删除创建 Collection View 的三行并设置 dataSourcedelegate

次要但您的 homeFooterView 类应命名为 HomeFooterView。类名应以大写字母开头。

关于ios - Swift - 在没有 Storyboard initWithFrame 错误的情况下创建 collectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51052499/

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