标头中显示当前收藏夹视图图片

时间:2018-07-26 02:45:39

标签: ios swift

我正在尝试设计一个看起来像这样的视图

enter image description here

在这里,我有一个UIImageViewcollectionView中显示所选图像。这样做的问题是,向下滚动UIImageView时位于视图的顶部。

我正在尝试一种不同的方法,该方法在标头中使用UIImageView

这是我的视图,蓝色标题是UIImageView

enter image description here

是否可以在集合视图中将标题图片更改为所选图片,并将viewDidLoad中的所选图片设置为索引0。

1 个答案:

答案 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()