我正在创建一个没有情节提要的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
我的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")
}
}
答案 0 :(得分:3)
您的viewDidLoad
方法遇到一些问题。
HomeVC
类注册为标题视图类型。您要使用HeaderView.self
,而不是HomeVC.self
。viewWillTransition
方法更新布局。UICollectionView
。会在调用viewDidLoad
之前为您完成。删除创建集合视图的三行,并设置dataSource
和delegate
。未成年人,但您的homeFooterView
类应命名为HomeFooterView
。类名应以大写字母开头。