UICollectionView 必须使用非零布局参数初始化'
Posted
技术标签:
【中文标题】UICollectionView 必须使用非零布局参数初始化\'【英文标题】:UICollectionView must be initialized with a non-nil layout parameter'UICollectionView 必须使用非零布局参数初始化' 【发布时间】:2016-10-29 13:26:26 【问题描述】:我试图在视图控制器中使用两个集合视图,结果收到此错误消息?你能指出我缺少什么来纠正这个问题吗?提前致谢.. 下面是我在名为“OtherViewController”的视图控制器中的代码。
类 OtherViewController : UIViewController, UICollectionViewDataSource, UICollectionViewDelegate
//1. CREATE A VARIABLE FOR YELLOW ARRAY SO THAT I CAN DEFINE A STRING
var exploreArray = [String]()
var exploreHeader = [String]()
var yellowImages = [String]()
var explorecategories = [String]()
@IBOutlet weak var mycollectionView: UICollectionView!
var collectionViewA = UICollectionView()
let collectionViewB = UICollectionView()
let collectionViewAIdentifier = "yellowCell"
let collectionViewBIdentifier = "yellowCell2"
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
collectionViewA.delegate = self
collectionViewB.delegate = self
collectionViewA.dataSource = self
collectionViewB.dataSource = self
self.view.addSubview(collectionViewA)
self.view.addSubview(collectionViewB)
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
if collectionView == self.collectionViewA
return exploreArray.count
return 1 // Replace with count of your data for collectionViewB
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
if collectionView == self.collectionViewA
let cellA = collectionView.dequeueReusableCell(withReuseIdentifier: "yellowCell", for: indexPath) as! yellowCell
// Set up cell
//DISPLAY TITLE LABEL
cellA.yLabel.text = exploreHeader[indexPath.row]
//DISPLAY IMAGES
cellA.yellowImages.image = UIImage(named:yellowImages [indexPath.row])
//DISPLAY DESCRIPTION
cellA.yellowTextField.text = exploreArray [indexPath.row]
return cellA
else
let cellB = collectionView.dequeueReusableCell(withReuseIdentifier: "yellowCell2", for: indexPath) as! yellowCell2
cellB.labe2.text = "Dolphin"
//只是随机标签来查看cellB是否会出现在collection view中
return cellB
【问题讨论】:
【参考方案1】:我认为我对this question 的回答也可能对您的情况有所帮助。
具体来说,你在哪里
var collectionViewA = UICollectionView()
let collectionViewB = UICollectionView()
建议(有几个比我聪明得多的人!)改用它
var collectionViewA: UICollectionView!
var collectionViewB: UICollectionView!
然后在 viewDidLoad() 中,连同设置委托和数据源(如您所做的那样),添加类似
let layoutA = UICollectionViewFlowLayout()
layoutA.itemSize = CGSize(width: 100, height: 100)
let layoutB = UICollectionViewFlowLayout()
layoutB.itemSize = CGSize(width: 100, height: 100)
collectionViewA = UICollectionView(frame: self.view.frame, collectionViewLayout: layoutA)
collectionViewB = UICollectionView(frame: self.view.frame, collectionViewLayout: layoutB)
最后在添加子视图之后,
collectionViewLeft.register(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewLeftIdentifier)
collectionViewRight.register(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewRightIdentifier)
完整代码显示在链接问题的答案中。 (使用 left & right 而不是 A & B,但希望同样的想法适用。)
【讨论】:
以上是关于UICollectionView 必须使用非零布局参数初始化'的主要内容,如果未能解决你的问题,请参考以下文章
UICollectionView 必须使用非零布局参数初始化
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“UICollectionView 必须使用非零布局参数初始化
Swift 中的 UICollectionView 自定义布局
使用自定义布局重新加载 UICollectionView 会引发错误
在 numberOfItemsInSection 返回非零项目编号后未调用 UICollectionView cellForItemAtIndexPath