Dynamically set the color collectionview label background color

I have the following implementation, which works it expected. I wonder how should I also deal with color of the corresponding label. For example, I want to set label color to blue for active, green for success and red for rejected

enum Status: Int, RawRepresentable, CustomStringConvertible {
  case active
  case success
  case rejected
  
  var description : String {
    switch self {
    case .active:
      return "Active"
    case .success:
      return "Success"
    case .rejected:
      return "Rejected"
    }
  }
}

ClassCollectionViewCell.swift

final class ClassCollectionViewCell : UICollectionViewCell {
  @IBOutlet weak var statusLabel: UILabel!
}

ViewController

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ClassCollectionViewCell.identifier, for: indexPath) as? ClassCollectionViewCell else { return UICollectionViewCell() }
  cell.statusLabel.text = classVM.getStatusText(atIndex: indexPath.row)
  return cell
}

ViewModel

func getStatusText(atIndex index: Int) -> String {
  return courses[index].status ?? ""
}

Courses.swift

struct Courses: Codable {
  let status: String?
}


from Recent Questions - Stack Overflow https://ift.tt/3qUfkPI
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)