swift UICollectionView显示空有时甚至是bug头

时间:2017-05-04 09:29:22

标签: swift uicollectionview uicollectionviewcell uicollectionreusableview

我有一个带有页眉和页脚的UIcollectionView,有时候一个奇怪的错误只会显示屏幕为空。这很奇怪,因为即使在View Debugger上它显示为空,但是Header是一个静态图像,它在应用程序上并没有从API获得,也没有显示。

此外,控制台不提供任何错误方法。我尝试转到另一个视图并强制reloadData()但仍然没有显示任何内容。我可以用任何方式更好地调试它或确保它不会发生吗?

这是视图调试器的样子:

this is how the view debugger looks like

您可以看到页眉和页脚空可重复使用的视图:

you can see the header and footer empty

修改

这是使用RxDataSources

创建补充视图的方式
 dataSource.supplementaryViewFactory = { (dataSource, collectionView, kind, indexPath) in
        switch kind {
        case UICollectionElementKindSectionHeader:
            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: CellReuseId.contentHeaderCollectionView, for: indexPath ) as! ContentHeaderView
            headerView.imageView.image = ImageAssets.contentBanner
            let tapGestureRecognizer = UITapGestureRecognizer(target:self, action: #selector(self.showListOfEvents(_:)))
            headerView.addGestureRecognizer(tapGestureRecognizer)
            return headerView

        case UICollectionElementKindSectionFooter:
            let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: CellReuseId.contentFooterCollectionView, for: indexPath) as! ContentFooterView
            footerView.setUpObjects()
            //setUp Appropiate label or animation
            let showFooter: Bool = !(self.centerActivityIndicator?.isAnimating ?? false)
            footerView.setUpAppropiateDependingOnResults(isThereMoreResults: self.isThereMoreResults, showFooter: showFooter)
            return footerView
        default: break
        }
        //return default
        return UICollectionReusableView()
    }

这是获取API模型并将它们绑定到collectionView

的代码
let results = Observable.of(paginationObserver, offlineObserver).merge()
    //calls method that calls DB with the appropiate data
        .flatMapLatest { [unowned self] parametersChanged -> Driver<LecturesStateResults> in
            //since this is being called again, we make sure to clean out "old cache data" on view model
            self.videoObject.lecturesResults.value.removeAll(keepingCapacity: true)
            return self.setupLectures().asDriver(onErrorJustReturn: LecturesStateResults.empty)
        }

    results
        //Bind the result observable to the UIcollectionView
        //UiCollection view only wants an array not an Observable
        .map {
            if !$0.results.isEmpty { self.centerActivityIndicator?.stopAnimating()}
            return [SectionModel(model: "", items: $0.results)]
        }
        .bind(to: lectureViewSquare.rx.items(dataSource: dataSource))
        .addDisposableTo(disposeBag)

    lectureViewSquare.rx.setDelegate(self)
        .addDisposableTo(disposeBag)

1 个答案:

答案 0 :(得分:1)

我在使用相同的ui渲染时遇到了同样的问题,但没有使用RxSwift。

解决问题的代码是

dispatch_async(dispatch_get_main_queue(), ^{
      [self.collectionView reloadData];
});