简体   繁体   中英

UICollectionView NSInternalInconsistencyException error on iOS9 only

I am getting this on iOS9, but on iOS10 everything works fine:

Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.60.12/UICollectionView.m:1599

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set' libc++abi.dylib: terminating with uncaught exception of type NSException

EDIT: My code

//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
    }
}

One collection view is inside 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: I rebuild whole UIViewController (deleted old one and made new one), but still getting this error.

Ooookay, so I had method

func setupCell() {
    self.layoutIfNeeded()
    if self.gradientView.layer.sublayers == nil {
        self.gradientView.drawGradient(colors: [whiteColor.cgColor, colorWithAlpha(whiteColor, alpha: 0.5).cgColor], locations: [0, 1])
    }
    self.mapView.isMyLocationEnabled = false
    self.mapView.settings.myLocationButton = false
    self.locationManager.delegate = self
    self.nearyLabel.addCharactersSpacing(1.94, text: "Рядом")
    if CLLocationManager.authorizationStatus() == .authorizedAlways || CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
        self.locationManager.startUpdatingLocation()
    } else {
        let coordinateCenter = CLLocationCoordinate2DMake(37.606384, 55.765191)
        self.mapView.camera = GMSCameraPosition(target: coordinateCenter, zoom: 14, bearing: 0, viewingAngle: 0)
    }
}

And the problem was self.layoutIfNeeded() , I removed this line and everything is fine. I had no idea what happened, cause iOS10, as I said, is fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM