How to use an enum and switch() with UITableViewController in Swift

后端 未结 3 977
刺人心
刺人心 2021-01-13 17:51

My UITableView has two sections, so I created an enum for them:

private enum TableSections {
    HorizontalSection,
    VerticalSection
}

H

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-13 18:18

    Jeff Lewis did it right, to elaborate on that and give the code litlle bit of more readiness -> my way of handling these things is to:

    1. Instantiate enum with the raw value -> section index

    guard let sectionType = TableSections(rawValue: section) else { return 0 }

    1. Use switch with section type

    switch sectionType { case .horizontalSection: return firstArray.count case .verticalSection: return secondArray.count }

提交回复
热议问题