[英]Swift - creating collectionView without Storyboard initWithFrame error
我正在创建一个没有情节提要的collectionView-从我的项目中删除了它。
我在AppDelegate中创建它。 我的页脚和页眉已正确出队,在加载图像时显示的单元格为零,但是我无法弄清楚initWithFrame方法的调用位置! 任何帮助表示赞赏。
不使用故事板
在应用程序委托内部调用
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
我的单元格,页脚,页眉都已出队。 我无法弄清楚这是哪里出错了...这里是安装代码:
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
方法中存在一些问题。
HomeVC
类注册为标题视图类型。 您想使用HeaderView.self
而不是HomeVC.self
。 viewWillTransition
方法中更新布局。 UICollectionView
。 会在调用viewDidLoad
之前为您完成。 删除创建收集视图的三行,并设置dataSource
和delegate
。 较小,但您的homeFooterView
类应命名为HomeFooterView
。 类名应以大写字母开头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.