【问题标题】:swift UICollectionView showing empty sometimes bug even headerswift UICollectionView 有时会显示空的错误甚至标题
【发布时间】:2017-05-04 09:29:22
【问题描述】:

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

控制台也没有给出任何错误方法。我尝试转到另一个视图并强制执行 reloadData() 但仍然没有显示任何内容。有什么办法可以更好地调试它或确保它不会发生?

这是视图调试器的样子:

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

编辑:

这是使用 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)

【问题讨论】:

  • 你能给我们看一些代码吗?
  • 这有点复杂,因为我定义了 UICollectionView 和 UICollectionCell 并且我使用 RxSwift 将数据绑定到表。我也在使用 UICollectionViewDelegateFlowLayout
  • 您使用的是 Storyboard 还是 Xib 的?我猜你重写了 CollectionView 数据源方法,不是吗?
  • 是的,RxSwift 覆盖了这些方法,这可能是问题所在吗?
  • 您能在此处粘贴您将数据源与collectionView 绑定的代码吗?即便如此,看看 RxDatasources,因为我不能 100% 确定,但你不能用 RxSwift 设置你自己的 UICollectionReusableViews(你需要 RxDataSources)。

标签: swift uicollectionview uicollectionviewcell uicollectionreusableview


【解决方案1】:

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

解决问题的代码是

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

【讨论】:

    猜你喜欢
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多