2017-08-30 46 views
0

我有一个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为特定的旅程类型填充单元格。

+0

你的问题是什么?发生什么事?为什么在你的csee中使用关联int值而不是枚举值?你问如何将int从Web服务转换为枚举值?如果是这样,请使用rawValue – Paulw11

+0

@ Paulw11请参阅我从webservice获取Journey Type的值为“单向”。所以我需要检查值是否是单向我应该放置“单向”的另一个单元格的自定义单元格。 –

+0

你问的是如何初始化一个字符串rawValue的枚举值?你有没有阅读Swift Book中关于枚举的章节? – Paulw11

回答

1

如果我正确理解您的问题,您想要使用Web服务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的情况下

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! 
    } 
} 
相关问题