仅在iOS9上出现UICollectionView NSInternalInconsistencyException错误

时间:2016-11-22 12:29:14

标签: ios swift uicollectionview

我在iOS9上得到这个,但在iOS10上一切正常:

  

断言失败 - [UICollectionView   _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:],   /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.60.12/UICollectionView.m:1599

     

由于未捕获的异常而终止应用   'NSInternalInconsistencyException',原因:'UICollectionView   dataSource未设置为'libc ++ abi.dylib:以uncaught终止   NSException类型的异常

编辑:我的代码

//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(删除旧的并创建了新的),但仍然收到此错误。

1 个答案:

答案 0 :(得分:1)

Ooookay,所以我有方法

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

问题是self.layoutIfNeeded(),我删除了这一行,一切都很好。我不知道发生了什么,导致iOS10,正如我所说,很好。