Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save conscientiousness/b46d7df0c0ae4e7b26f5fd9f9dd574c6 to your computer and use it in GitHub Desktop.
Save conscientiousness/b46d7df0c0ae4e7b26f5fd9f9dd574c6 to your computer and use it in GitHub Desktop.
//=====定義Enum=====
enum MyListType: Int, CustomStringConvertible, EnumCaseCountable {
case waitingForReceive
case overseaReceived
case shipmentNotified
case notifyTransferPay
case transferPayChecked
case overseaShipped
case shipmentDone
var description: String {
switch self {
case .waitingForReceive:
return R.string.localizable.myTransfer_main_list_waitingForReceive_text.key.localized()
case .overseaReceived:
return R.string.localizable.myTransfer_main_list_overseaReceived_text.key.localized()
case .shipmentNotified:
return R.string.localizable.myTransfer_main_list_shipmentNotified_text.key.localized()
case .notifyTransferPay:
return R.string.localizable.myTransfer_main_list_notifyTransferPay_text.key.localized()
case .transferPayChecked:
return R.string.localizable.myTransfer_main_list_transferPayChecked_text.key.localized()
case .overseaShipped:
return R.string.localizable.myTransfer_main_list_overseaShipped_text.key.localized()
case .shipmentDone:
return R.string.localizable.myTransfer_main_list_shipmentDone_text.key.localized()
}
}
var icon: String {
switch self {
case .waitingForReceive:
return "ic_my_transform_status1"
case .overseaReceived:
return "ic_my_transform_status2"
case .shipmentNotified:
return "ic_my_transform_status3"
case .notifyTransferPay:
return "ic_my_transform_status4"
case .transferPayChecked:
return "ic_my_transform_status5"
case .overseaShipped:
return "ic_my_transform_status6"
case .shipmentDone:
return "ic_my_transform_status7"
}
}
static let caseCount = MyListType.countCases()
}
//=====定義Countable=====
// enum which provides a count of its cases
protocol EnumCaseCountable {
static func countCases() -> Int
static var caseCount : Int { get }
}
// provide a default implementation to count the cases for Int enums assuming starting at 0 and contiguous
extension EnumCaseCountable where Self : RawRepresentable, Self.RawValue == Int {
// count the number of cases in the enum
static func countCases() -> Int {
// starting at zero, verify whether the enum can be instantiated from the Int and increment until it cannot
var count = 0
while let _ = Self(rawValue: count) { count += 1 }
return count
}
}
//=====使用=====
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return MyListType.countCases()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as MyTransferMainCell
cell.configCell(type: MyTransferMainListType(rawValue: indexPath.row) ?? .waitingForReceive)
return cell
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment