简体   繁体   English

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

[英]How to assign the custom cell in UITableView using Enum in Swift3?

I have got a UITableView which has got 2 custom cells. 我有一个UITableView有2个自定义单元格。 The type of the custom cell is defined by the key "JourneyType" which we are getting from the web service. 自定义单元格的类型由我们从Web服务获取的键“ JourneyType”定义。 So if the Journey Type is Solo we need to show the cell with SoloCustomCell and if the journey type is Group we need to show the cell with GroupCustomCell. 因此,如果“旅程类型”是“独奏”,则需要使用SoloCustomCell显示单元格;如果旅程类型是“群体”,则需要显示具有GroupCustomCell的单元格。 I am in the idea of using enum for determining the Switch case for allocating the cells. 我的想法是使用枚举来确定用于分配单元的Switch情况。 I have defined the enum like this: 我已经定义了这样的枚举:

  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: "")
    }
  }
}

Now in the cellForRowIndex I have defined the code like this: 现在在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 is the value which we are getting from the Web service. Subscription.journeyType是我们从Web服务获得的值。 Currently subscription array contains 8 elements each having the journeyType. 当前,订阅数组包含8个元素,每个元素具有tripingType。 So we need to check the case based on the enum defined at the above. 因此,我们需要根据上面定义的枚举检查情况。 How to populate the cell for specific journey type based on the Enum. 如何根据枚举填充特定旅程类型的单元格。

If I understand your question correctly, you want to initialise an enumeration value using a string from a web services API. 如果我正确理解了您的问题,则希望使用Web服务API中的字符串初始化枚举值。

The easiest way to do that is to use a string raw value with your enumeration. 最简单的方法是在枚举中使用字符串原始值。

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

Now, you can initialise an instance by saying something like: 现在,您可以通过执行类似以下操作来初始化实例:

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

You can then use the trip type to determine a tableview cell and as your switch statement can be exhaustive you don't need a default case 然后,您可以使用行程类型来确定表视图单元格,并且由于switch语句可以穷举,因此不需要default情况

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!
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM