我正在尝试设计一个看起来像这样的视图
在这里,我有一个UIImageView
在collectionView
中显示所选图像。这样做的问题是,向下滚动UIImageView
时位于视图的顶部。
我正在尝试一种不同的方法,该方法在标头中使用UIImageView
。
这是我的视图,蓝色标题是UIImageView
。
是否可以在集合视图中将标题图片更改为所选图片,并将viewDidLoad
中的所选图片设置为索引0。
答案 0 :(得分:0)
我找到了解决方案,但是我不确定这是否是正确的方法。
我创建了一个全局变量并将其设置为0 var selectedIndex: Int = 0
将标题单元出队时,我将索引设置为selectedInex
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionFooter:
let footer = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionFooter, withReuseIdentifier: footerIdentifier, for: indexPath) as! footerCell
footer.backgroundColor = UIColor.blue
return footer
case UICollectionElementKindSectionHeader:
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerIdentifier, for: indexPath) as! headerCell
header.imageView.image = images[selectedInex]
return header
default:
fatalError("Unexpected element kind")
}
}
在didSelectItemAt
selectedInex = indexPath.row
collectionView.reloadData()