extension CaseIterable where Self: RawRepresentable {

    static var allValues: [RawValue] {
        return allCases.map { $0.rawValue }
    }
    
    static func toValue(index: Int) -> Self? {
        if let selected = Self.allValues[safe: index] {
            return Self(rawValue: selected)
        }
        return nil
    }
}

// MARK: CellTypeAdapter - CollectionView/TableView Cell Type Adapter
protocol CellTypeAdapter {
    var identifier: String { get }
    func nib() -> UINib
    
}

extension CellTypeAdapter {
    var identifier: String {
        return String(describing: Self.self)
    }
    func nib() -> UINib {
        return UINib(nibName: self.identifier, bundle: nil)
    }
}

// MARK: 공통 메뉴리스트 타입 (TabaleView List Cell Adapter)
enum ListMenuCellType {
    case BasicMenu([String])
    case SelectMenu([String], Int)
}

extension ListMenuCellType: CellTypeAdapter {
    var identifier: String {
        switch self {
        case .BasicMenu:
            return MenuListCell.identifier
        case .SelectMenu:
            return MenuListCell.identifier
        }
    }
    
    func nib() -> UINib {
        return UINib(nibName: self.identifier, bundle: nil)
    }
    
    /// Cell Inset Top / Bottom 반영된 수치 값
    var rowHeight: CGFloat {
        switch self {
        case .BasicMenu:
            return 55.0     // + Spacing top : 5.0, bottom : 5.0
        case .SelectMenu:
            return 55.0     // + Spacing top : 5.0, bottom : 5.0
        }
    }
    
    /*
      - 컬렉션뷰와 다르게 Cell Spacing 개념이 없음.
      - Cell ContentView 간격을 강제로 변경시 사용하려고 했으나 셀 컨텐츠에서 간격 조정하는걸로...
    */
    var contentViewInset: UIEdgeInsets {
        switch self {
        case .BasicMenu:
            return UIEdgeInsets(top: 5.0, left: 0.0, bottom: 5.0, right: 0.0)
        case .SelectMenu:
            return UIEdgeInsets(top: 5.0, left: 0.0, bottom: 5.0, right: 0.0)
        }
    }
}

 

enum ViewsSettingMenu: String, CaseIterable {
    case V3000          = "3000"
    case V1000          = "1000"
    case V800           = "800"
    case V500           = "500"
    case V300           = "300"
    case V100           = "100"
}

let menuCellType: ListMenuCellType = .SelectMenu(ViewsSettingMenu.allValues, 1)


switch menuCellType {
	case .BasicMenu(let menus):
		print("menu list \(menus)")
	case .SelectMenu(let menus, let selectIndex):
		print("menu list \(menus), SelectIndex \(selectIndex)")
	default: break
}

+ Recent posts