Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct MyModel: Equatable {
- var id: Int
- var showSomething: Bool
- var someText: String
- }
- class CellOne: UICollectionViewCell {}
- class CellTwo: UICollectionViewCell {}
- class FooClass: UIViewController {
- var collectionView: UICollectionView!
- var dataSource: [MyModel] = []
- //Update your data source and reload collectionView
- func updateData(with item: MyModel, for index: Int) {
- var item = item
- dataSource.enumerated().forEach {
- if $0.element.id == item.id {
- item.showSomething.toggle()
- dataSource[$0.offset] = item
- collectionView.reloadData()
- }
- }
- }
- }
- extension FooClass: UICollectionViewDelegate {
- func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- //When some action triggered, update your dataSource and reload collectionview
- let selectedItem = dataSource[indexPath.row]
- updateData(with: selectedItem, for: indexPath.row)
- }
- }
- extension FooClass: UICollectionViewDataSource {
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- dataSource.count
- }
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- let model = dataSource[indexPath.row]
- let cell: UICollectionViewCell
- if model.showSomething {
- cell = CellOne()
- //some logic update for cellOne
- } else {
- cell = CellTwo()
- //some logic update for cellTwo
- }
- return cell
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement