1

I need a UICollection view to show 1 of 2 sets of data upon selection. I'd like to use a segmented control but could use separate buttons as well. How do I configure the view controller to show the selected collection or implement 2 data sources in one collection view and implement switch?

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    switch control.selectedSegmentIndex {
    case 0 :
        guard let cell = collectionView.dequeueReusableCell(
        withReuseIdentifier: PhotoCollectionViewCell.identifier,
        for: indexPath
    ) as? PhotoCollectionViewCell else {
        fatalError()
    }
    
    cell.configure(with: URL(string: posts[indexPath.row].postUrlString))
    return cell
    case 1:
        guard let cell = collectionView.dequeueReusableCell(
            withReuseIdentifier: VideoCollectionViewCell.identifier,
            for: indexPath
        ) as? VideoCollectionViewCell else {
            fatalError()
        }
        
        cell.configure(with: URL(string: posts[indexPath.row].postUrlString))
        return cell
    default:
        guard let cell = collectionView.dequeueReusableCell(
            withReuseIdentifier: PhotoCollectionViewCell.identifier,
            for: indexPath
        ) as? PhotoCollectionViewCell else {
            fatalError()
        }
        
        cell.configure(with: URL(string: posts[indexPath.row].postUrlString))
        return cell
    }
}

@objc private func controlTapped(_ sender: UISegmentedControl) {
    switch control.selectedSegmentIndex {
    case 0:
        collectionView?.reloadData()
        return
    case 1:
        collectionView?.reloadData()
        return
    default:
        collectionView?.reloadData()
    }
}

EDIT:

I've edited the cellForItemAt function as instructed.

DavidJames
  • 11
  • 3
  • 1
    Does this answer your question? [How can I add multiple collection views in a UIViewController in Swift?](https://stackoverflow.com/questions/28750108/how-can-i-add-multiple-collection-views-in-a-uiviewcontroller-in-swift) – Andrew Apr 10 '21 at 05:27
  • Can you clarify what you want to do? The suggested duplicate is about showing two collection views in the one view controller, but it sounds like you want a single collection view but to be able to choose between two sets of data, which is slightly different. In that case you simply need to check which data set you should be displaying in all of the data source methods (`numberOfItemsInSection` etc) and return the correct data. – Paulw11 Apr 10 '21 at 13:13
  • I want a single collection view but to be able to choose between two sets of data. I configured the data source methods ```numberOfItemsInSection``` etc. but am having trouble implementing the switch around my current configuration. – DavidJames Apr 10 '21 at 20:22
  • Your `numberOfItems` looks ok. You need similar logic in `cellForItemAt` (and you should use `indexPath.item` for a collection view). You will also need to reload the collection view when the selected segment changes. – Paulw11 Apr 10 '21 at 20:26
  • I've edited ```cellForItemAt``` as instructed. I don't think my switch function is registering the change. Would you be interested in teaming up to build the next great app? :) – DavidJames Apr 10 '21 at 20:42
  • Are you sure you want to be using `posts` in `case 1` and not `videoPosts`? Judging by the code that you removed when you edited last, this looks like it might be the case. – jnpdx Apr 10 '21 at 20:54

0 Answers0