I am creating a table view and inside a custom UItableviewCell I am using collection view and I also create a custom collectionview cell. After setting all the basic functionalities of collection view in TableViewCell, When run the app I got the crash with reason:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the view returned from -collectionView:cellForItemAtIndexPath: ( {length = 2, path = 0 - 0}) was not retrieved by calling -dequeueReusableCellWithReuseIdentifier:forIndexPath: or is nil (>)'
I tried to search more for it but can't find any direction
Here is my code snippet: 1. In TableViewcell awakeFrom Nib:
override func awakeFromNib() { super.awakeFromNib() // self.collectionView_Files.registerNib(UINib(nibName: "MediaCollectionCell", bundle: nil), forCellWithReuseIdentifier: "MediaCollectionCell") self.collectionView_Files.registerClass(MediaCollectionCell.self, forCellWithReuseIdentifier: "MediaCollectionCell") }
CollectionView methods:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return arrFolderData.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let simpleTableIdentifier = "MediaCollectionCell" var cell: MediaCollectionCell = collectionView.dequeueReusableCellWithReuseIdentifier(simpleTableIdentifier, forIndexPath: indexPath) as! MediaCollectionCell cell = NSBundle.mainBundle().loadNibNamed(simpleTableIdentifier, owner: self, options: nil)[0] as! (MediaCollectionCell) let dict = arrFolderData[indexPath.row] if(dict["file_type"] as! String == "0") { /// Means image cell.imgView_Item.image = UIImage(named: "images_41") cell.btn_ViewImage.hidden = false cell.btn_PlayVideo.hidden = true } else if (dict["file_type"] as! String == "1") { /// Means video cell.btn_ViewImage.hidden = true cell.btn_PlayVideo.hidden = false cell.imgView_Item.image = UIImage(named: "video_thumb_icon") } // cell.backgroundColor = arrFolderData[indexPath.item] return cell } func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { print("Collection view at row \(collectionView.tag) selected index path \(indexPath)") } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { let length = (UIScreen.mainScreen().bounds.width-15)/2 return CGSizeMake(length,length); }