问题描述
我有一个UICollectionView
,并且启用了页眉和页脚:
在此处设置页脚的高度:
func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,referenceSizeforFooterInSection section: Int) -> CGSize {
if isLoading {
return CGSize.zero
}
return CGSize(width: collectionView.bounds.size.width,height: 55)
}
...然后在此处设置页眉和页脚:
func collectionView(_ collectionView: UICollectionView,viewForSupplementaryElementOfKind kind: String,at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionView.elementKindSectionFooter {
let aFooterView = collectionView.dequeueReusableSupplementaryView(ofKind: kind,withReuseIdentifier: footerViewReuseIdentifier,for: indexPath) as! CustomFooterView
self.footerView = aFooterView
self.footerView?.backgroundColor = UIColor.green
return aFooterView
} else {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader,withReuseIdentifier: "HeaderView",for: indexPath) as! CollectionViewHeader
return headerView
}
}
并在此处注册它们:
collectionView?.register(CollectionViewHeader.self,forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,withReuseIdentifier: "HeaderView")
collectionView?.register(UINib(nibName: "CustomFooterView",bundle: nil),forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter,withReuseIdentifier: footerViewReuseIdentifier)
当我将集合视图设置为使用默认布局时,可以看到页眉和页脚。当我将其设置为下面的自定义布局时,我只会看到页眉,而看不到页脚,并且为什么会这样:
class ColumnFlowLayout: UICollectionViewFlowLayout {
private let minColumnWidth: CGFloat = 300.0
private let cellHeight: CGFloat = 184.0
private var deletingIndexPaths = [IndexPath]()
private var insertingIndexPaths = [IndexPath]()
// MARK: Layout Overrides
/// - Tag: ColumnFlowExample
override func prepare() {
super.prepare()
guard let collectionView = collectionView else { return }
let availableWidth = collectionView.bounds.inset(by: collectionView.layoutMargins).width
let maxnumColumns = Int(availableWidth / minColumnWidth)
let cellWidth = (availableWidth / CGFloat(maxnumColumns)).rounded(.down)
self.itemSize = CGSize(width: cellWidth,height: cellHeight)
self.sectionInset = UIEdgeInsets(top: self.minimumInteritemSpacing,left: 0.0,bottom: 0.0,right: 0.0)
self.sectionInsetReference = .fromSafeArea
}
// MARK: Attributes for Updated Items
override func finalLayoutAttributesFordisappearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
guard let attributes = super.finalLayoutAttributesFordisappearingItem(at: itemIndexPath) else { return nil }
if !deletingIndexPaths.isEmpty {
if deletingIndexPaths.contains(itemIndexPath) {
attributes.transform = CGAffineTransform(scaleX: 0.5,y: 0.5)
attributes.alpha = 0.0
attributes.zIndex = 0
}
}
return attributes
}
override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
guard let attributes = super.initialLayoutAttributesForAppearingItem(at: itemIndexPath) else { return nil }
if insertingIndexPaths.contains(itemIndexPath) {
attributes.transform = CGAffineTransform(scaleX: 0.5,y: 0.5)
attributes.alpha = 0.0
attributes.zIndex = 0
}
return attributes
}
// MARK: Updates
override func prepare(forCollectionViewUpdates updateItems: [UICollectionViewUpdateItem]) {
super.prepare(forCollectionViewUpdates: updateItems)
for update in updateItems {
switch update.updateAction {
case .delete:
guard let indexPath = update.indexPathBeforeUpdate else { return }
deletingIndexPaths.append(indexPath)
case .insert:
guard let indexPath = update.indexPathAfterUpdate else { return }
insertingIndexPaths.append(indexPath)
default:
break
}
}
}
override func finalizeCollectionViewUpdates() {
super.finalizeCollectionViewUpdates()
deletingIndexPaths.removeAll()
insertingIndexPaths.removeAll()
}
}
EDIT (出于某些原因,如果我设置了
collectionView.collectionViewLayout = ColumnFlowLayout()
,页脚为零,但如果我未将布局设置为ColumnFlowLayout
,则页脚分配正确。
编辑2 可以在此委托方法中确认这一点:
func collectionView(_ collectionView: UICollectionView,for: indexPath) as! CollectionViewHeader
return headerView
}
}
当我有:
collectionView.collectionViewLayout = ColumnFlowLayout()
,
程序永远无法到达
if kind == UICollectionView.elementKindSectionFooter
,但确实可以解决collectionView.collectionViewLayout = ColumnFlowLayout()
被注释掉的情况。
解决方法
弄清楚了;需要在ColumnFlowLayout
实现中手动设置页脚高度,如下所示:
self.footerReferenceSize = CGSize(width: w,height: h)