简体   繁体   English

Swift-创建没有Storyboard initWithFrame错误的collectionView

[英]Swift - creating collectionView without Storyboard initWithFrame error

I am creating a collectionView without a storyboard - removed it from my project. 我正在创建一个没有情节提要的collectionView-从我的项目中删除了它。

I create it here, in AppDelegate. 我在AppDelegate中创建它。 My footer and header are dequeued correctly, I have zero cells displaying as images are loading, but I cannot figure out where this method initWithFrame is called! 我的页脚和页眉已正确出队,在加载图像时显示的单元格为零,但是我无法弄清楚initWithFrame方法的调用位置! Any help appreciated. 任何帮助表示赞赏。

NOT USING STORYBOARD 不使用故事板

called inside app 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)

This ultimately fails with the following error: 最终将失败,并显示以下错误:

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

I have my Cell, Footer, Header all dequeued. 我的单元格,页脚,页眉都已出队。 I cannot figure out where this is erroring... Heres setup code: 我无法弄清楚这是哪里出错了...这里是安装代码:

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")


}
}

You have a few issues in your viewDidLoad method. 您的viewDidLoad方法中存在一些问题。

  1. Your primary issue is registering your HomeVC class as the header view type. 您的主要问题是将HomeVC类注册为标题视图类型。 You want to use HeaderView.self , not HomeVC.self . 您想使用HeaderView.self而不是HomeVC.self
  2. Do not base the layout on screen size. 不要将布局基于屏幕尺寸。 Your collection view may not take up the entire screen. 您的收藏夹视图可能不会占据整个屏幕。 Base the layout on the size of the collection view. 布局基于集合视图的大小。 Also note that the size could change. 另请注意,大小可能会改变。 It's best to update the layout in the viewWillTransition method. 最好在viewWillTransition方法中更新布局。
  3. This is a UICollectionViewController. 这是一个UICollectionViewController。 You should not be creating your own UICollectionView . 您不应该创建自己的UICollectionView It will be done for you before viewDidLoad is called. 会在调用viewDidLoad之前为您完成。 Remove the three lines that create the collection view and set the dataSource and delegate . 删除创建收集视图的三行,并设置dataSourcedelegate

Minor but your homeFooterView class should be named HomeFooterView . 较小,但您的homeFooterView类应命名为HomeFooterView Class names should start with uppercase letters. 类名应以大写字母开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM