swift UICollectionView 显示空有时甚至 header
swift UICollectionView showing empty sometimes bug even header
我有一个 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 ![=12=].results.isEmpty { self.centerActivityIndicator?.stopAnimating()}
return [SectionModel(model: "", items: [=12=].results)]
}
.bind(to: lectureViewSquare.rx.items(dataSource: dataSource))
.addDisposableTo(disposeBag)
lectureViewSquare.rx.setDelegate(self)
.addDisposableTo(disposeBag)
我在使用相同的 ui 渲染时遇到了同样的问题,但没有使用 RxSwift。
解决问题的代码是
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
我有一个 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 ![=12=].results.isEmpty { self.centerActivityIndicator?.stopAnimating()}
return [SectionModel(model: "", items: [=12=].results)]
}
.bind(to: lectureViewSquare.rx.items(dataSource: dataSource))
.addDisposableTo(disposeBag)
lectureViewSquare.rx.setDelegate(self)
.addDisposableTo(disposeBag)
我在使用相同的 ui 渲染时遇到了同样的问题,但没有使用 RxSwift。
解决问题的代码是
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});