我得到這個在iOS9,但iOS10一切正常:UICollectionView NSInternalInconsistencyException錯誤只
斷言失敗 - [UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:], /buildroot的/圖書館/緩存/ com.apple.xbs /資源/的UIKit/UIKit的-3512.60.12/UICollectionView.m:1599
終止應用程序由於未捕獲的異常 'NSInternalInconsistencyException',理由是:「UICollectionView 數據源爲n OT集」的libC++ abi.dylib:與未捕獲 異常類型NSException
EDIT的終止:我的代碼
//also have all dataSources in storyboard, this func just to be clear, called in viewDidLoad
func delegates() {
self.nearbyRestarauntsCollectionView.dataSource = self
self.topCollectionView.dataSource = self
self.restOfTheWeek.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
switch collectionView {
case topCollectionView:
return dataForBestChoise.count
case nearbyRestarauntsCollectionView:
return dataForNearest.count
case restOfTheWeek:
return dataForTop.count
default:
return groupedSelection["\(selectionTypes[collectionView.tag])"]!.count
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
switch collectionView {
case topCollectionView:
return CGSize(width: UIScreen.main.bounds.width, height: 292)
case nearbyRestarauntsCollectionView:
return CGSize(width: 190, height: 196)
case restOfTheWeek:
return CGSize(width: UIScreen.main.bounds.width, height: 230)
default:
return CGSize(width: 150, height: 180)
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch collectionView {
case topCollectionView:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "topCollectionViewCell", for: indexPath) as! MainTopCollectionViewCell
if UIScreen.main.bounds.width < 375 {
cell.ratingCenterOutlet.constant = 5
} else {
cell.ratingCenterOutlet.constant = 20
}
cell.setupCell(data: dataForBestChoise[indexPath.row])
return cell
case nearbyRestarauntsCollectionView:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "nearbyRestarauntsCell", for: indexPath) as! NearbyRestarauntsCollectionViewCell
cell.setupCell(data: dataForNearest[indexPath.row])
return cell
case restOfTheWeek:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "restsOfTheWeek", for: indexPath) as! RestsOfTheWeekCollectionViewCell
cell.setupCell(data: dataForTop[indexPath.row])
if defaults.bool(forKey: "isLogged") {
if dataForTop[indexPath.row].isFavourited {
cell.isFavourited.isHidden = false
cell.isFavourited.image = UIImage(named: "isFavouritedTrue")
} else {
cell.isFavourited.isHidden = false
cell.isFavourited.image = UIImage(named: "selectionIsNotFav")
}
} else {
cell.isFavourited.isHidden = true
}
cell.isFavourited.isUserInteractionEnabled = true
let imageTap = UITapGestureRecognizer(target: self, action: #selector(self.addRestToFavourites))
imageTap.cancelsTouchesInView = true
imageTap.numberOfTapsRequired = 1
cell.isFavourited.addGestureRecognizer(imageTap)
cell.isFavourited.tag = indexPath.row
return cell
default:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "bottomSelectionCVCell", for: indexPath) as! BottomSelectionCollectionViewCell
cell.setupSelection(tag: collectionView.tag, row: indexPath.row, selection: (groupedSelection["\(selectionTypes[collectionView.tag])"]?[indexPath.row])!)
if defaults.bool(forKey: "isLogged") == true {
if cell.isFavourited == true {
cell.favImage.isHidden = false
cell.favImage.image = UIImage(named: "isFavouritedTrue")
} else {
cell.favImage.isHidden = false
cell.favImage.image = UIImage(named: "selectionIsNotFav")
}
} else {
cell.favImage.isHidden = true
}
cell.favImage.isUserInteractionEnabled = true
let imageTap = UITapGestureRecognizer(target: self, action: #selector(self.addSelectionToFavourites))
imageTap.cancelsTouchesInView = true
imageTap.numberOfTapsRequired = 1
cell.favImage.addGestureRecognizer(imageTap)
return cell
}
}
一個集合視圖是內部的tableView:
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.selectionTypes.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "bottomSelectionCell", for: indexPath) as! BottomSelectionsTableViewCell
cell.selectionGroupName.text = self.selectionTypes[indexPath.row].uppercased()
cell.selectionGroupName.addCharactersSpacing(1.95, text: cell.selectionGroupName.text!)
cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
return cell
}
func setCollectionViewDataSourceDelegate <D: UICollectionViewDataSource & UICollectionViewDelegate> (_ dataSourceDelegate: D, forRow row: Int) {
collectionView.delegate = dataSourceDelegate
collectionView.dataSource = dataSourceDelegate
collectionView.tag = row
collectionView.reloadData()
}
EDIT2 : 我重建整個UIViewController
(刪除舊的和新的),但仍然得到這個錯誤。
在您的問題中添加第1599行的代碼 –
NSInternalInconsistencyException在此處說您尚未設置數據源。 –
也許UICollectionView dataSource沒有設置? – shallowThought