首页 > 解决方案 > 重新加载 UICollectionView viewForSupplementaryElementOfKind 而不重新加载 cellForItemAt

问题描述

我只想重新加载footerView。如果我使用 reloadData(),整个 collectionView 都会重新加载。

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

        switch kind {

        case UICollectionView.elementKindSectionHeader:

            if let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: MockupCollectionHeader.className, for: indexPath) as? MockupCollectionHeader {
            headerView.configure()
                headerView.titleLabel.text = baseLayout.properties?.titleString
            return headerView
            }
            return UICollectionReusableView()


        case UICollectionView.elementKindSectionFooter:

            if let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: MockupCollectionFooter.className, for: indexPath) as? MockupCollectionFooter {
                footerView.configure()
                footerView.footerLabel.text = baseLayout.properties?.footerBody
                footerView.continueButton.addTarget(self, action: #selector(continueClicked(_:)), for: UIControl.Event.touchUpInside)
                footerView.checkButton.addTarget(self, action: #selector(checkboxClicked(_:)), for: UIControl.Event.touchUpInside)
                footerView.isChecked = baseLayout.isCheckbox
            return footerView
            }
            return UICollectionReusableView()


        default:

            assert(false, "Unexpected element kind")
        }
        
        return UICollectionReusableView()
}

标签: swiftswift5

解决方案


推荐阅读