【发布时间】:2015-06-25 17:25:52
【问题描述】:
所以我正在开发一个需要在一个 CollectionViewController 中包含两个集合视图的应用程序。但是由于某种原因,每当我运行它时,我都会收到错误:
2015-06-25 13:23:23.601 Quorum[35215:6966756] * -[Quorum.ManageListsCollectionViewController loadView]、/SourceCache/UIKit/UIKit-3347.44/UICollectionViewController.m:171 中的断言失败 2015-06-25 13:23:23.604 Quorum[35215:6966756] * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:'-[UICollectionViewController loadView] 加载了“5Oy-7X-AZf-view- 1bs-84-zCb" nib 但没有获得 UICollectionView。 *** 首先抛出调用堆栈: (0x183e982d8 0x1956bc0e4 0x183e98198 0x184d4ced4 0x188a57f24 0x1888d8a28 0x18898ef68 0x18898ee64 0x18898e2f0 0x18898df9c 0x18898dcbc 0x18898dc3c 0x1888d5760 0x18821de1c 0x188218884 0x188218728 0x188217ebc 0x188217c3c 0x1888cc56c 0x183e502a4 0x183e4d230 0x183e4d610 0x183d792d4 0x18d58f6fc 0x18893efac 0x10007aca8 0x195d3aa08) libc++abi.dylib:以 NSException 类型的未捕获异常终止
这是我的 CollectionViewController 代码:
import UIKit
let reuseIdentifier = "Cell"
class ManageListsCollectionViewController: UICollectionViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var menuButton: UIBarButtonItem!
@IBOutlet weak var ListView: UICollectionView!
@IBOutlet weak var DetailView: UICollectionView!
let ListViewIdentifier = "ListViewCell"
let DetailViewIdentifier = "DetailViewCell"
let sectionInsets = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0)
override func viewDidLoad() {
super.viewDidLoad()
if self.revealViewController() != nil {
menuButton.target = self.revealViewController()
menuButton.action = "revealToggle:"
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
ListView.delegate = self
DetailView.delegate = self
ListView.dataSource = self
DetailView.dataSource = self
self.view.addSubview(ListView)
self.view.addSubview(DetailView)
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
// MARK: UICollectionViewDataSource
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//#warning Incomplete method implementation -- Return the number of sections
if collectionView == self.ListView {
return 1
} else{
return 1
}
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//#warning Incomplete method implementation -- Return the number of items in the section
if collectionView == self.ListView {
return 20
}
return 5
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if collectionView == self.ListView {
let cell: ListCell = collectionView.dequeueReusableCellWithReuseIdentifier(ListViewIdentifier, forIndexPath: indexPath) as! ListCell
// Set up cell
return cell
}
else {
let cell1: DetailCell = collectionView.dequeueReusableCellWithReuseIdentifier(DetailViewIdentifier,forIndexPath: indexPath) as! DetailCell
// ...Set up cell
return cell1
}
【问题讨论】:
-
似乎没有任何 UICollectionView 实例连接到控制器的视图属性,这是通过情节提要加载控制器时所需的
-
如果你需要在同一个视图控制器中有两个集合视图,我相信你需要使用一个普通的
UIViewController和两个UICollectionViews。你不能使用UICollectionViewController。 -
@AdamPro13 为什么会这样?
标签: ios swift cocoa-touch uicollectionview