我有一个UITableView有2个自定义单元格。自定义单元格的类型由我们从Web服务获得的关键字“JourneyType”定义。因此,如果旅程类型为Solo,我们需要使用SoloCustomCell显示单元格,如果旅程类型为Group,则需要使用GroupCustomCell显示单元格。我想用enum来确定分配单元的Switch情况。我已经定义枚举这样的:如何在Swift3中使用Enum在UITableView中分配自定义单元格?
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服务所获得的价值。当前订阅数组包含8个元素,每个元素具有journeyType。所以我们需要根据上面定义的枚举来检查案例。如何根据Enum为特定的旅程类型填充单元格。
你的问题是什么?发生什么事?为什么在你的csee中使用关联int值而不是枚举值?你问如何将int从Web服务转换为枚举值?如果是这样,请使用rawValue – Paulw11
@ Paulw11请参阅我从webservice获取Journey Type的值为“单向”。所以我需要检查值是否是单向我应该放置“单向”的另一个单元格的自定义单元格。 –
你问的是如何初始化一个字符串rawValue的枚举值?你有没有阅读Swift Book中关于枚举的章节? – Paulw11