ios - 如何修复 UICollectionView 单元格中的致命错误?
问题描述
我MasterViewController
有. UICollectionView
_ storyboard
我将此代码用于我的UICollectionView
:
主视图控制器:
class MasterViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, URLSessionDownloadDelegate, SKProductsRequestDelegate, SKStoreProductViewControllerDelegate {
@IBOutlet weak var collectionView: UICollectionView?
fileprivate var currentPage: Int = 0
fileprivate var pageSize: CGSize {
let layout = UPCarouselFlowLayout()
var pageSize = layout.itemSize
pageSize.width += layout.minimumLineSpacing
return pageSize
}
override func viewDidLoad() {
super.viewDidLoad()
self.addCollectionView()
self.setupLayout()
}
func setupLayout(){
let pointEstimator = RelativeLayoutUtilityClass(referenceFrameSize: self.view.frame.size)
self.collectionView?.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
self.collectionView?.topAnchor.constraint(equalTo: self.view.topAnchor, constant: pointEstimator.relativeHeight(multiplier: 0.1754)).isActive = true
self.collectionView?.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
self.collectionView?.heightAnchor.constraint(equalToConstant: pointEstimator.relativeHeight(multiplier: 0.6887)).isActive = true
self.currentPage = 0
}
func addCollectionView(){
let pointEstimator = RelativeLayoutUtilityClass(referenceFrameSize: self.view.frame.size)
let layout = UPCarouselFlowLayout()
layout.itemSize = CGSize(width: pointEstimator.relativeWidth(multiplier: 0.73333), height: 400)
layout.scrollDirection = .horizontal
self.collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
self.collectionView?.translatesAutoresizingMaskIntoConstraints = false
self.collectionView?.delegate = self
self.collectionView?.dataSource = self
self.collectionView?.register(MasterViewCell.self, forCellWithReuseIdentifier: "Cell")
let spacingLayout = UPCarouselFlowLayout()
spacingLayout.spacingMode = UPCarouselFlowLayoutSpacingMode.overlap(visibleOffset: 20)
}
主视图单元:
class MasterViewCell: UICollectionViewCell {
let customView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = 12
return view
}()
var cellImageView: UIImageView!
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(self.customView)
self.customView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
self.customView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
self.customView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1).isActive = true
self.customView.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 1).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
相对布局实用程序类:
class RelativeLayoutUtilityClass {
var heightFrame: CGFloat?
var widthFrame: CGFloat?
init(referenceFrameSize: CGSize){
heightFrame = referenceFrameSize.height
widthFrame = referenceFrameSize.width
}
func relativeHeight(multiplier: CGFloat) -> CGFloat{
return multiplier * self.heightFrame!
}
func relativeWidth(multiplier: CGFloat) -> CGFloat{
return multiplier * self.widthFrame!
}
但我有这个错误MasterViewCell
:*Thread 1: Fatal error: init(coder:) has not been implemented*
如何解决?
解决方案
您还没有实现init(coder:)
初始化程序,如您在此处看到的:
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
您似乎正在从情节提要中加载一些单元格,因此您不能只fatalError
在此处使用。
实现应该非常简单。您可以执行与在init(frame:)
初始化程序中执行的操作相同的操作:
class MasterViewCell: UICollectionViewCell {
let customView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = 12
return view
}()
var cellImageView: UIImageView!
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
self.addSubview(self.customView)
self.customView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
self.customView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
self.customView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1).isActive = true
self.customView.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 1).isActive = true
}
}
推荐阅读
- mysql - 从 3 个表中总结并分组?
- css - 使用 react-native 在用户界面中放置一个特定于平台的按钮
- c++ - 如何在 C++ 中从键盘读取可变数量的整数?
- r - 将文本数据转换为表格/csv 格式
- python - 导入 .txt 文件时将字符串转换为 int
- jquery - ready() 工作,on('ready') 不工作,为什么?
- html - 在按钮单击时创建允许/阻止浏览器请求
- django - Django 中的 JWT 配置
- java - 尝试将新的 dialogFragment 添加到片段上的现有 dialogfragmen 时,在空对象引用上的 FragmentManager.beginTransaction()
- javascript - KnockoutJS - 使用选择 2 的多选仅显示第一个选择