简体   繁体   中英

Swift - creating collectionView without Storyboard initWithFrame error

I am creating a collectionView without a storyboard - removed it from my project.

I create it here, in 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! 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.

  1. Your primary issue is registering your HomeVC class as the header view type. You want to use HeaderView.self , not 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.
  3. This is a UICollectionViewController. You should not be creating your own UICollectionView . It will be done for you before viewDidLoad is called. Remove the three lines that create the collection view and set the dataSource and delegate .

Minor but your homeFooterView class should be named HomeFooterView . Class names should start with uppercase letters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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