为什么我的 UICollectionViewController 不会 运行 有两个 CollectionViews?
Why will my UICollectionViewController not run with two CollectionViews?
所以我正在开发一个应用程序,它需要在一个 CollectionViewController 中包含两个集合视图。但出于某种原因,每当我 运行 它时,我都会收到错误消息:
2015-06-25 13:23:23.601 Quorum[35215:6966756] * Assertion failure in -[Quorum.ManageListsCollectionViewController loadView], /SourceCache/UIKit/UIKit-3347.44/UICollectionViewController.m:171
2015-06-25 13:23:23.604 Quorum[35215:6966756] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UICollectionViewController loadView] loaded the "5Oy-7X-AZf-view-1bs-84-zCb" nib but didn't get a UICollectionView.'
*** First throw call stack:
(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: terminating with uncaught exception of type 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
}
如果你需要在一个父视图中有两个集合视图,并且你想使用集合视图控制器来管理这些集合,你需要使用控制器包含。您可以在 Interface Builder 中非常轻松地进行设置。
所以我正在开发一个应用程序,它需要在一个 CollectionViewController 中包含两个集合视图。但出于某种原因,每当我 运行 它时,我都会收到错误消息:
2015-06-25 13:23:23.601 Quorum[35215:6966756] * Assertion failure in -[Quorum.ManageListsCollectionViewController loadView], /SourceCache/UIKit/UIKit-3347.44/UICollectionViewController.m:171 2015-06-25 13:23:23.604 Quorum[35215:6966756] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UICollectionViewController loadView] loaded the "5Oy-7X-AZf-view-1bs-84-zCb" nib but didn't get a UICollectionView.' *** First throw call stack: (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: terminating with uncaught exception of type 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
}
如果你需要在一个父视图中有两个集合视图,并且你想使用集合视图控制器来管理这些集合,你需要使用控制器包含。您可以在 Interface Builder 中非常轻松地进行设置。