我一直在尝试在我的视图Controller中添加UICollectionView时遇到问题。这是我在尝试运行应用程序时遇到的错误:
当我运行我的应用程序时我得到此错误:NSInternalInconsistencyException',原因:' - [UICollectionViewController loadView]实例化视图控制器,标识符为“UIViewController-UZJ-6r-Rwt”,来自故事板“Main”,但没有得到一个UICollectionView“。会导致此错误的原因是什么?
这是我对View Controller的代码。
import UIKit
class ViewController: UICollectionViewController {
var names = ["a","d","f","f","h","u","p","s","l","g","y","h","l","k","d"]
var images = [UIImage(named: "title205571580"),UIImage(named: "title193064553"),UIImage(named: "title311505831"),UIImage(named: "title351533554"),UIImage(named: "title528311821"),UIImage(named: "title528311821"),UIImage(named: "title543192229"),UIImage(named: "title544380635"),UIImage(named: "title579492173"),UIImage(named: "title673858816"),UIImage(named: "title713937166"),UIImage(named: "title718520594"),UIImage(named: "title743923696"),UIImage(named: "title800939906"), UIImage(named: "title987804505")]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 15
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "uicvcell", for: indexPath) as! UICVCell
let image = cell.viewWithTag(1) as! UIImageView
image.image = images[indexPath.row]
let name = cell.viewWithTag(2) as! UILabel
name.text = names [indexPath.row]
return cell
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if(indexPath.row == Int("0")){
performSegue(withIdentifier: "0", sender: nil)
}
else if(indexPath.row == Int("1")){
performSegue(withIdentifier: "1", sender: nil)
}
else if(indexPath.row == Int("2")){
performSegue(withIdentifier: "2", sender: nil)
}
else if(indexPath.row == Int("3")){
performSegue(withIdentifier: "3", sender: nil)
}
else if(indexPath.row == Int("4")){
performSegue(withIdentifier: "4", sender: nil)
}
else if(indexPath.row == Int("5")){
performSegue(withIdentifier: "5", sender: nil)
}
else if(indexPath.row == Int("6")){
performSegue(withIdentifier: "6", sender: nil)
}
else if(indexPath.row == Int("7")){
performSegue(withIdentifier: "7", sender: nil)
}
else if(indexPath.row == Int("8")){
performSegue(withIdentifier: "8", sender: nil)
}
else if(indexPath.row == Int("9")){
performSegue(withIdentifier: "9", sender: nil)
}
else if(indexPath.row == Int("10")){
performSegue(withIdentifier: "10", sender: nil)
}
else if(indexPath.row == Int("11")){
performSegue(withIdentifier: "11", sender: nil)
}
else if(indexPath.row == Int("12")){
performSegue(withIdentifier: "12", sender: nil)
}
else if(indexPath.row == Int("13")){
performSegue(withIdentifier: "13", sender: nil)
}
else if(indexPath.row == Int("14")){
performSegue(withIdentifier: "14", sender: nil)
}
else if(indexPath.row == Int("15")){
performSegue(withIdentifier: "15", sender: nil)
}
else if(indexPath.row == Int("16")){
performSegue(withIdentifier: "16", sender: nil) }
else if(indexPath.row == Int("17")){
performSegue(withIdentifier: "17", sender: nil)
}
else if(indexPath.row == Int("18")){
performSegue(withIdentifier: "18", sender: nil)
}
}
}
答案 0 :(得分:3)
看起来你是UICollectionViewController
的子类。故事板中的VC是什么?或者只是一个普通的UIViewController?如果您在故事板中将其创建为UICollectionViewController,则UICollectionView已经存在。如果你需要添加UICollectionView,那么你可能创建了一个常规的UIViewController,然后在UICollectionView中添加 - 这很好,但是你需要改变你的源来反映它。
答案 1 :(得分:0)
看起来您的控制器缺少collectionView
属性。您需要做的是将其设置为.storyboard
文件中的插座。如果你想从代码中设置它,你需要做这样的事情:
override func viewDidLoad() {
super.viewDidLoad()
// Create your custom collectionView.
let myCollectionView = MyCollectionView()
// Set it as outlet.
collectionView = myCollectionView
}
答案 2 :(得分:0)
缺少的是你在故事板中使用了UIViewController。删除它并确保使用UICollectionViewController。