user9226105
user9226105

Reputation:

How can I make an array that holds types?

I am trying to create an array that holds the class of the custom collectionView cell I would like to deque. Below I have provided an example of how I would like to use this array. cellType is a variable holding the class I would like to deque and cellClass is an array holding different classes. I have seen similar questions to this but all the answers seem to suggest using the an instance of the class such as className.self. Is it possible to create an array like this. Thank You.

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cellType = cellClass[indexPath.item]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as! cellType

        cell.addRemoveCellDelegate = self
        cell.label.text = "\(indexPath)"
        switch indexPath.item {
        case 0:
            cell.backgroundColor = .magenta
            cell.screenLabel.text = screens[0]
            case 1:
            cell.backgroundColor = .purple
            cell.screenLabel.text = screens[1]
            case 2:
            cell.backgroundColor = .yellow
            cell.screenLabel.text = screens[2]
            case 3:
            cell.backgroundColor = .green
            cell.screenLabel.text = screens[3]
        default:
            cell.backgroundColor = .blue
        }
        return cell
    }

Upvotes: 1

Views: 137

Answers (1)

Alex Kolovatov
Alex Kolovatov

Reputation: 879

First of all I suggest you to create a manager file.

import Foundation

class CollectionViewManager: NSObject {

  public var cells: [CellModel] = []

  override init() {
      super.init()

      fillCells()
 }

  fileprivate func fillCells() {
      let arrayCellModels: [CellModel] = [
          RowModel(type: .cellOne, title: "My First Cell"),
          RowModel(type: .cellTwo, title: "My Second Cell"),
          RowModel(type: .cellThree, title: "My Third Cell")
      ]

      arrayCellModels.forEach { (cell) in
          cells.append(cell)
      }
  }

}

protocol CellModel {
  var type: CellTypes { get }
  var title: String { get }
}

enum CellTypes {
  case cellOne
  case cellTwo
  case cellThree
}

struct RowModel: CellModel {
  var type: OptionsCellTypes
  var title: String

  init(type: CellTypes, title: String) {
      self.type = type
      self.title = title
  }

}

After that in your ViewController you should initialise your manager. Something like that.

class ViewController: UICollectionViewController {

  let collectionViewManager = CollectionViewManager()

  // your code here
}

Next you make an ViewController extension.

extension ViewController: UICollectionViewDelegateFlowLayout {

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // number of items from your array of models
    return collectionViewManager.cells.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    // init item
    let item = collectionViewManager.cells[indexPath.item]

    // than just switch your cells by type 
    switch item.type {
    case .cellOne:
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(CellOne.self), for: indexPath) as! CellOne {
        cell.backgroundColor = .red
        return cell
    case .cellTwo:
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(CellTwo.self), for: indexPath) as! CellTwo {
        cell.backgroundColor = .blue
        return cell
    case .cellThree
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(CellThree.self), for: indexPath) as! CellThree {
        cell.backgroundColor = .yellow
        return cell
        }
}

}

Upvotes: 2

Related Questions