大佬教程收集整理的这篇文章主要介绍了根据 CollectionView 中的数组计数,节数不起作用,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有 collectionVIEw,如果我的两个数组计数都大于 0,我想显示两个部分。如果一个数组计数为 0,另一个数组计数大于 0,则显示 1 个部分,否则显示 0 个部分。但我的问题是我的一个数组计数大于 0 仍然没有节。
func numberOfSections(in collectionVIEw: UICollectionVIEw) -> Int {
if collectionVIEw == specialityCollection {
return 1
} else if collectionVIEw == recentSearchCollection
{
if (self.recentSearchValue?.count ?? 0 > 0) && (self.pastConsultationList?.count ?? 0 > 0) {
return 2
} else if (self.recentSearchValue?.count ?? 0 > 0) && (self.pastConsultationList?.count == 0) {
return 1
} else if (self.pastConsultationList?.count ?? 0 > 0) && (self.recentSearchValue?.count == 0) {
return 1
} else {
return 0
}
} else {
return 0
}
}
func collectionVIEw(_ collectionVIEw: UICollectionVIEw,numberOfItemsInSection section: int) -> Int {
if collectionVIEw == specialityCollection {
return self.SpecialtyList?.count ?? 0
} else {
if section == 0 {
return self.recentSearchValue?.count ?? 0
} else {
return self.pastConsultationList?.count ?? 0
}
}
}
func collectionVIEw(_ collectionVIEw: UICollectionVIEw,vIEwForSupplementaryElementOfKind kind: String,at indexPath: IndexPath) -> UICollectionReusableVIEw {
guard let sectionheaderVIEw = collectionVIEw.dequeueReusableSupplementaryVIEw(ofKind: kind,withReusEIDentifIEr: "headerVIEw",for: indexPath) as? headerVIEw else { return UICollectionReusableVIEw()}
if indexPath.section == 0 {
if self.recentSearchValue?.count ?? 0 > 0 {
sectionheaderVIEw.headertitle.text = self.headertitle[indexPath.section]
} } else {
if self.pastConsultationList?.count ?? 0 > 0 {
sectionheaderVIEw.headertitle.text = self.headertitle[indexPath.section] } }
return sectionheaderVIEw
}
问题在于当属性为 nil 时的相等比较,因为 nil 不等于 为零,因此如果 pastConsultationList
为 nil,下面的内容将始终为 false
pastConsultationList?.count == 0
所以改成
pastConsultationList?.count ?? 0 == 0
当然recentSearchValue
也是如此
话虽如此,我发现整个内部的 if/else
子句有点凌乱,我认为可以用更简洁的方式表达,例如这样
let recentSearch = recentSearchValue?.count ?? 0 > 0 ? 1 : 0
let pastConsultation = pastConsultationList?.count ?? 0 > 0 ? 1: 0
return recentSearch + pastConsultation
或者首先使用现在的 AND 条件,然后使用完全相同的条件,但使用 OR 代替。
以上是大佬教程为你收集整理的根据 CollectionView 中的数组计数,节数不起作用全部内容,希望文章能够帮你解决根据 CollectionView 中的数组计数,节数不起作用所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。