首次使用枚举开关,所以有几个问题。
我希望在tableView函数中使用此switch语句。首先,在使用枚举开关之前,我是否声明变量是否打开?如果是这样,我是否将open变量传递给交换机或使用新名称创建交换机并传入open变量?第三,如何从交换机接收值?
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FCT") as! FoodCellTwo
let each = resultss[indexPath.row]
var open: GMSPlacesOpenNowStatus = each.openNowStatus
enum open : Int {
/** The place is open now. */
case yes
/** The place is not open now. */
case no
/** We don't know whether the place is open now. */
case unknown
}
cell.nameLabel.text = each.name
return cell
}
答案 0 :(得分:1)
以下是如何使用枚举
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let status = openStatus // Get you open status or you could just use switch(openStatus)
switch status {
case .yes:
cell.textLabel?.text = "This places is open"
case .no:
cell.textLabel?.text = "This places is closed now"
case .unknown:
cell.textLabel?.text = "No idea about open status"
}
return cell
}
或强>
我建议你像这样在GMSPlacesOpenNowStatus
上写一个扩展名
extension GMSPlacesOpenNowStatus {
func getStringTitle() -> String {
switch self {
case .yes:
return "This places is open"
case .no:
return "This places is closed now"
case .unknown:
return "No idea about open status"
}
}
}
并使用此扩展名
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let status = openStatus
cell.textLabel?.text = status.getStringTitle()
return cell
}