diff --git a/Example/Sources/List/ListViewController.swift b/Example/Sources/List/ListViewController.swift index f89c146..95c6a9b 100644 --- a/Example/Sources/List/ListViewController.swift +++ b/Example/Sources/List/ListViewController.swift @@ -40,7 +40,6 @@ final class ListViewController: ExampleViewController, CellEventCoordinator { // In this example, the cell view models handle cell selection and navigation themselves. - // MARK: View lifecycle override func viewDidLoad() { diff --git a/Example/Sources/Main/ExampleViewController.swift b/Example/Sources/Main/ExampleViewController.swift index b31ab08..278130f 100644 --- a/Example/Sources/Main/ExampleViewController.swift +++ b/Example/Sources/Main/ExampleViewController.swift @@ -50,6 +50,11 @@ class ExampleViewController: UIViewController { self.addReloadButton() } + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + self.collectionView.deselectAllItems() + } + // MARK: Actions @objc diff --git a/ReactiveCollectionsKit.xcodeproj/project.pbxproj b/ReactiveCollectionsKit.xcodeproj/project.pbxproj index 7dbe5f2..6ac467d 100644 --- a/ReactiveCollectionsKit.xcodeproj/project.pbxproj +++ b/ReactiveCollectionsKit.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 0B01476F2BBCF0330041D432 /* Collection+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B01476E2BBCF0330041D432 /* Collection+Extensions.swift */; }; + 0B25D73A2BFAD31C00EB3664 /* UICollectionView+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B25D7392BFAD31C00EB3664 /* UICollectionView+Extensions.swift */; }; 0B31B9312BCC5B66006F2078 /* CellEventCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B31B9302BCC5B66006F2078 /* CellEventCoordinator.swift */; }; 0B8C6CEC267ED08E0042DFE2 /* SupplementaryViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B8C6CEB267ED08E0042DFE2 /* SupplementaryViewModel.swift */; }; 0BC1EC28267FC37200FF774F /* SupplementaryHeaderViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0BC1EC27267FC37200FF774F /* SupplementaryHeaderViewModel.swift */; }; @@ -50,6 +51,7 @@ /* Begin PBXFileReference section */ 0B01476E2BBCF0330041D432 /* Collection+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Collection+Extensions.swift"; sourceTree = ""; }; + 0B25D7392BFAD31C00EB3664 /* UICollectionView+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UICollectionView+Extensions.swift"; sourceTree = ""; }; 0B31B9302BCC5B66006F2078 /* CellEventCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CellEventCoordinator.swift; sourceTree = ""; }; 0B8C6CEB267ED08E0042DFE2 /* SupplementaryViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SupplementaryViewModel.swift; sourceTree = ""; }; 0BC1EC27267FC37200FF774F /* SupplementaryHeaderViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SupplementaryHeaderViewModel.swift; sourceTree = ""; }; @@ -162,6 +164,7 @@ 0BC1EC29267FC3A000FF774F /* SupplementaryFooterViewModel.swift */, 0BC1EC27267FC37200FF774F /* SupplementaryHeaderViewModel.swift */, 0B8C6CEB267ED08E0042DFE2 /* SupplementaryViewModel.swift */, + 0B25D7392BFAD31C00EB3664 /* UICollectionView+Extensions.swift */, 0BC1EC2D267FF46E00FF774F /* ViewRegistration.swift */, 0BFA774F2BB92E36009B9C5B /* ViewRegistrationMethod.swift */, 0BC1EC2B267FE71900FF774F /* ViewRegistrationProvider.swift */, @@ -332,6 +335,7 @@ 0BC1EC2C267FE71900FF774F /* ViewRegistrationProvider.swift in Sources */, 88FA49082363A7890061F8B2 /* CollectionViewModel.swift in Sources */, 0BFA774E2BB92DC8009B9C5B /* ViewRegistrationViewType.swift in Sources */, + 0B25D73A2BFAD31C00EB3664 /* UICollectionView+Extensions.swift in Sources */, 0B01476F2BBCF0330041D432 /* Collection+Extensions.swift in Sources */, 88FA490A2363A7890061F8B2 /* CellViewModel.swift in Sources */, 0BC1EC28267FC37200FF774F /* SupplementaryHeaderViewModel.swift in Sources */, diff --git a/Sources/CollectionViewDriver.swift b/Sources/CollectionViewDriver.swift index 87c7a07..790f497 100644 --- a/Sources/CollectionViewDriver.swift +++ b/Sources/CollectionViewDriver.swift @@ -163,7 +163,8 @@ public final class CollectionViewDriver: NSObject { private func _cellProvider( collectionView: UICollectionView, indexPath: IndexPath, - identifier: UniqueIdentifier) -> UICollectionViewCell { + identifier: UniqueIdentifier + ) -> UICollectionViewCell { let cell = self.viewModel.cellViewModel(for: identifier) precondition(cell != nil, "Inconsistent state. Cell with identifier \(identifier) does not exist.") return cell!.dequeueAndConfigureCellFor(collectionView: collectionView, at: indexPath) @@ -172,7 +173,8 @@ public final class CollectionViewDriver: NSObject { private func _supplementaryViewProvider( collectionView: UICollectionView, elementKind: String, - indexPath: IndexPath) -> UICollectionReusableView? { + indexPath: IndexPath + ) -> UICollectionReusableView? { let supplementaryView = self.viewModel.supplementaryViewModel(ofKind: elementKind, at: indexPath) return supplementaryView?.dequeueAndConfigureViewFor(collectionView: collectionView, at: indexPath) } diff --git a/Sources/UICollectionView+Extensions.swift b/Sources/UICollectionView+Extensions.swift new file mode 100644 index 0000000..9509a51 --- /dev/null +++ b/Sources/UICollectionView+Extensions.swift @@ -0,0 +1,23 @@ +// +// Created by Jesse Squires +// https://www.jessesquires.com +// +// Documentation +// https://jessesquires.github.io/ReactiveCollectionsKit +// +// GitHub +// https://github.com/jessesquires/ReactiveCollectionsKit +// +// Copyright © 2019-present Jesse Squires +// + +import Foundation +import UIKit + +extension UICollectionView { + public func deselectAllItems(animated: Bool = true) { + self.indexPathsForSelectedItems?.forEach { + self.deselectItem(at: $0, animated: animated) + } + } +}