ios - 如何在 Swift3 中使用 Enum 在 UITableView 中分配自定义单元格?

标签 ios swift uitableview enums

我有一个 UITableView,它有 2 个自定义单元格。自定义单元格的类型由我们从 Web 服务获取的键“JourneyType”定义。因此,如果旅程类型是 Solo,我们需要显示带有 SoloCustomCell 的单元格,如果旅程类型是 Group,我们需要显示带有 GroupCustomCell 的单元格。我的想法是使用枚举来确定用于分配单元格的 Switch case。我已经这样定义了枚举:

  enum SubscriptionTripType: Int {
  case oneWayTrip = 1
  case roundTrip
  case splitShift

  var value: String {
    switch self {
    case .oneWayTrip:
      return NSLocalizedString("One way", comment: "")
    case .roundTrip:
      return NSLocalizedString("Round trip", comment: "")
    case .splitShift:
      return NSLocalizedString("Split shift", comment: "")
    }
  }
}

现在在 cellForRowIndex 中我定义了这样的代码:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let subscription = mySubscriptionDetails[indexPath.row]
    switch subscription.journeyType{
    case 0:
      let proposePoolcell = tableView.dequeueReusableCell(withIdentifier: "subscriptionCell", for: indexPath) as? MySubscriptionCustomCell
      configure(cell: proposePoolcell!, forRowAtIndexPath: indexPath)
      return proposePoolcell!
    case 1:
      let splitShiftCell = tableView.dequeueReusableCell(withIdentifier: "splitShiftCell", for: indexPath) as? SplitShiftSubscriptionCell
      return splitShiftCell!
    default:break
    }
  }

Subscription.journeyType 是我们从 Web 服务获取的值。当前 subscription 数组包含 8 个元素,每个元素都有 journeyType。所以我们需要根据上面定义的枚举来检查大小写。如何根据枚举为特定旅程类型填充单元格。

最佳答案

如果我正确理解您的问题,您想要使用来自网络服务 API 的字符串来初始化枚举值。

最简单的方法是在枚举中使用字符串原始值。

enum SubscriptionTripType: String {
    case oneWayTrip = "One Way"
    case roundTrip = "Round Trip"
    case splitShift = "Split Shift"
} 

现在,您可以通过这样的语句来初始化一个实例:

if let tripType = SubscriptionTripType(rawValue:"One Way") {
    print(tripType)
} else {
    print("Invalid trip type")
}

然后您可以使用行程类型来确定 tableview 单元格,并且由于您的 switch 语句可能是详尽无遗的,因此您不需要 default case

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let subscription = mySubscriptionDetails[indexPath.row]
    switch subscription.journeyType {
    case .oneWayTrip:
      let proposePoolcell = tableView.dequeueReusableCell(withIdentifier: "subscriptionCell", for: indexPath) as! MySubscriptionCustomCell
      configure(cell: proposePoolcell, forRowAtIndexPath: indexPath)
      return proposePoolcell

    case .roundTrip:
      let roundTripCell = tableView.dequeueReusableCell(withIdentifier: "roundTripCell", for: indexPath) as! MyRoundTripCustomCell
      configure(cell: proposePoolcell, forRowAtIndexPath: indexPath)
      return roundTripCell

    case .splitShift:
      let splitShiftCell = tableView.dequeueReusableCell(withIdentifier: "splitShiftCell", for: indexPath) as! SplitShiftSubscriptionCell
      return splitShiftCell!
    }
}

关于ios - 如何在 Swift3 中使用 Enum 在 UITableView 中分配自定义单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45969709/

相关文章:

ios - 任何使用 Scala 进行 iOS 编码的方法?

ios - 使用 NSString 以编程方式访问或创建 Objective-C 中的方法

ios - 带有 UITableViewController 的 SearchBar 包含错误数量的单元格

ios - reloadData 后调用 numberOfRowsInSection 的次数过多

ios - 所有可本地化字符串的字母顺序

iphone - 获取核心数据对象的子对象的属性?

ios - 在 Parse 数据库中查询具有多个指针的类

ios - 停止 UISearchBar 使用 UITableView 滚动

swift - xcode9/SceneKit - .dae 文件未加载到 SCNScene - 返回 nil

ios - SwiftUI View 中的间距不正确