首页 > 解决方案 > 尝试从更新前仅包含 27 个项目的第 0 节中删除项目 30

问题描述

*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“尝试从第 0 节中删除第 30 项,更新前仅包含 27 个项目”

当集合视图重新加载我的应用程序崩溃时,我遇到了一个问题。我的代码逻辑是,我正在显示一些单元格,并且每 10 个单元格添加一个额外的广告单元格,我认为这里出现问题,因为重新加载集合视图时缺少索引路径。我已经用谷歌搜索并获得了一些关于我的崩溃问题的信息。其中一些是 performBatchUpdates:completion 的文档说明:

在批处理操作中,在插入之前处理删除。这意味着删除的索引是相对于批处理操作之前集合视图状态的索引进行处理的,而插入的索引是相对于批处理操作中所有删除之后的状态索引进行处理的。

我无法在我的代码中实现它。有人可以帮我吗?谢谢。

 override func viewDidLoad(){

   super.viewDidLoad()

   DispatchQueue.main.async {

   self.viewModel.tilbudsappenModel.addProduct(userId: self.userInfo.userID, shopId: self.shopId)            
        self.m_CollectionVw.reloadData()
    }
}

override func viewWillAppear(_ animated: Bool){
SwiftEventBus.onMainThread(self, name: "ReloadTblVw") { _ in
        self.m_CollectionVw.reloadData()
    }
if self.isFollowed == true {
        viewModel.tilbudsappenModel.removeProduct()
        DispatchQueue.main.async {
            self.viewModel.tilbudsappenModel.addProduct(userId: self.userInfo.userID, shopId: self.shopId)
            self.m_CollectionVw.reloadData()
        }
    }
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // Here am getting the total items count
    numberOfOffers = viewModel.tilbudsappenModel.adNativeModel.totalItems()
    let totalNoOfOffers = numberOfOffers + (numberOfOffers / numberOfAds)
    return totalNoOfOffers        
 }


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if indexPath.row != 0 && indexPath.row % numberOfAds == 0 && !AppUserDefaults.SharedInstance.isSubscripted {
        let nativeAd: GADUnifiedNativeAd? = nil
        /// Set the native ad's rootViewController to the current view controller.
        nativeAd?.rootViewController = self

        let nativeAdCell = collectionView.dequeueReusableCell(withReuseIdentifier: "UnifiedNativeAdCell", for: indexPath)

        // Get the ad view from the Cell. The view hierarchy for this cell is defined in
        // UnifiedNativeAdCell.xib.
        let adView : GADUnifiedNativeAdView = nativeAdCell.contentView.subviews
            .first as! GADUnifiedNativeAdView

        if self.nativeAds.count > indexPath.row / numberOfAds {

            let nativeAd = self.nativeAds[indexPath.row / numberOfAds]
            adView.nativeAd = nativeAd

            nativeAd.delegate = self

            (adView.headlineView as? UILabel)?.text = nativeAd.headline
            adView.mediaView?.mediaContent = nativeAd.mediaContent
            // These assets are not guaranteed to be present. Check that they are before
            // showing or hiding them.
            (adView.bodyView as? UILabel)?.text = nativeAd.body
            adView.bodyView?.isHidden = nativeAd.body == nil

            (adView.callToActionView as? UIButton)?.setTitle(nativeAd.callToAction, for: .normal)
            adView.callToActionView?.isHidden = nativeAd.callToAction == nil

            (adView.iconView as? UIImageView)?.image = nativeAd.icon?.image
            adView.iconView?.isHidden = nativeAd.icon == nil

            (adView.starRatingView as? UIImageView)?.image = self.imageOfStars(from:nativeAd.starRating)
            adView.starRatingView?.isHidden = nativeAd.starRating == nil

            (adView.storeView as? UILabel)?.text = nativeAd.store
            adView.storeView?.isHidden = nativeAd.store == nil

            (adView.priceView as? UILabel)?.text = nativeAd.price
            adView.priceView?.isHidden = nativeAd.price == nil

            (adView.advertiserView as? UILabel)?.text = nativeAd.advertiser
            adView.advertiserView?.isHidden = nativeAd.advertiser == nil

            // In order for the SDK to process touch events properly, user interaction should be disabled.
            adView.callToActionView?.isUserInteractionEnabled = false

        }

        return nativeAdCell
    } else {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ShopOfferCell
        cellIndex = indexPath.item - indexPath.item / numberOfAds

        if let offerContent = viewModel.tilbudsappenModel.getProductItem(index: cellIndex) {
            cell.setContent(content: offerContent)
            if offerContent.imageWidth < Int(cell.contentView.frame.size.width) {
                cell.m_ImgVw.contentMode = .center
            }else {
                cell.m_ImgVw.contentMode = .scaleAspectFit
            }
        }
        return cell

    }

}

标签: iosuicollectionviewswift4uicollectionviewcellperformbatchupdates

解决方案


推荐阅读