在CollectionView中重新加载可见单元格时出现崩溃

时间:2017-09-08 08:02:25

标签: ios swift flurry collectionview flurry-analytics

我正在使用FlurryAd在collectionView中显示广告和图片。使用以下代码在后台线程中获取广告

func loadAds(count:Int,adType: Int) {
var newAdsList : [FlurryAdNative] = []
        DispatchQueue.global(qos: .background).async {
        if count >= 1 {
            for _ in 1...count {
                let nativeAd = FlurryAdNative(space: AD_SPACEID)
 nativeAd?.adDelegate  = self
                nativeAd?.viewControllerForPresentation = self
                nativeAd?.fetchAd()
                    if let adType = AdType(rawValue: adType) {
                        if adType == .OfferAd {
                            self.viewModel.tilbudsappenModel.adNativeModel.addNativeAds(item: nativeAd!)
                            newAdsList.append(nativeAd!)
                        }
                    }
            }
        }
            self.pendingAdList = newAdsList
        }
    }

当Ad准备就绪时,我尝试使用以下代码重新加载主线程中的可见单元格

func adNativeDidFetchAd(_ nativeAd: FlurryAdNative!) {
        let isScrolling = (self.m_CollectionVw.isDragging || self.m_CollectionVw.isDecelerating)
        if isScrolling == false {
            if let indexPath = self.m_CollectionVw?.indexPathsForVisibleItems {
                DispatchQueue.main.async {
                    self.m_CollectionVw?.reloadItems(at: indexPath)
                }
            }
        }


    }

然后因跟随错误而崩溃

*******断言失败 - [UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:],/ BuildRoot / Library / Cache / com.apple.xbs / Sources / UIKit_Sim / UIKit-3600.7.47 / UICollectionView.m :5781 ****

cellForItemAtindexPath看起来

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        switch viewModel.tilbudsappenModel.adNativeModel.cellType(index: indexPath.item + 1) {
        case .Ads:
            let cellIndex = indexPath.row  / viewModel.tilbudsappenModel.adNativeModel.adRangeIndex
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "adNativeCell", for: indexPath as IndexPath) as! NativeAdCell
            if  let adItem = viewModel.tilbudsappenModel.adNativeModel.nativeAdsItem(index: cellIndex) {
//                delay(0.01, closure: {
                    cell.setupWithFlurryNativeAd(adNative: adItem)
//                })

            }
            return cell
        case .Normal:
            let cellIndex = (indexPath.item + 1) * (viewModel.tilbudsappenModel.adNativeModel.adRangeIndex - 1)  / viewModel.tilbudsappenModel.adNativeModel.adRangeIndex
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! KategorierFollowCollectionViewCell
            cell.m_Label.text = String(format: "%d", indexPath.row)
            if let productContent = viewModel.tilbudsappenModel.getProductItem(index: cellIndex) {
                let followed = self.viewModel.tilbudsappenModel.getProductFollowed(prodId: productContent.id,userId: userInfo.userID)
                let added = self.viewModel.tilbudsappenModel.getProductAdded(prodId: productContent.id,userId: userInfo.userID)
                cell.setContent(content: productContent, isAdded: added, isFollowed: followed)
            }
            cell.backgroundColor = UIColor.white
            return cell
        }

    }

0 个答案:

没有答案