在.xib中创建的UICollectionView是零Swift

问题描述:

collectionView始终是零,我创建它在.xib中。 碰撞发生在与collectionView的第一次交互中 预先感谢您。在.xib中创建的UICollectionView是零Swift

这是错误。 致命错误:意外发现零而展开的可选值

class ChannelsViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, NSFetchedResultsControllerDelegate { 

    var managedObjectContext: NSManagedObjectContext? = AppDelegate.sharedAppDelegate().managedObjectContext; 

    @IBOutlet weak var collectionView: UICollectionView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.collectionView.delegate = self 
     self.collectionView.dataSource = self 

     self.collectionView.registerNib(UINib(nibName:"ChannelsCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "Cell") 
    } 
} 
+0

你确定UICollectionView有一个引用到xib中的collectionView属性的插座吗? – Acey 2014-08-30 18:31:43

+0

@Ayy是的,我确定。 – user3092406 2014-08-30 18:40:14

+0

在任何时候,您是否从其超级视图中移除collectionview? – Acey 2014-08-30 18:41:55

的问题,我是,我是不会加载笔尖。尝试这个。

init() { 
    super.init(nibName: "ChannelsViewController", bundle: nil) 
} 

required init(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.collectionView.delegate = self 
    self.collectionView.dataSource = self 

    self.collectionView.registerNib(UINib(nibName:"ChannelsCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "Cell") 
}