首页 > 解决方案 > 作为 TableView 数据源的 Swift 通用枚举

问题描述

我想创建一个 UITableViewController 接受enum作为它的数据源。问题是,我有很多enums我希望它能够处理的东西。

我创建了一个名为TableViewSelectable并创建了一些enums符合它的协议,如下所示:

protocol TableViewSelectable {

}

enum Genders: Int, TableViewSelectable {
    case male = 0, female

    static let allKeys = [male, female]
    static let allNames = [male.getName, female.getName]

    var getName: String {
        switch self {
        case .male:
            return "Male"
        case .female:
            return "Female"
        }
    }
}

enum Goals: Int, TableViewSelectable {
    case gainMuscleMass, getTrimFit, loseWeight

    static let allKeys = [gainMuscleMass, getTrimFit, loseWeight]
    static let allNames = [gainMuscleMass.getName, getTrimFit.getName, loseWeight.getName]

    var getName: String {
        switch self {
        case .gainMuscleMass:
            return "Gain muscle mass"
        case .getTrimFit:
            return "Get trim & fit"
        case .loseWeight:
            return "Lose weight"
        }
    }
}

我已经在我的UITableViewController喜欢上创建了一个实例变量:

class ChoiceListTableViewController: UITableViewController {

    var data: TableViewSelectable?

}

问题是我不知道如何从这里开始。我想要的是提供UITableViewController任何enum符合以TableViewSelectable用作其数据源的选项。

我想像这样访问它UITableViewController

final class ChoiceListTableViewController: UITableViewController {

    var data: TableViewSelectable?


    // MARK: Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()
        self.clearsSelectionOnViewWillAppear = false
    }

}

// MARK: Table View Data Source & Delegate

extension ChoiceListTableViewController {

    override func numberOfSections(in tableView: UITableView) -> Int {

        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return data.allKeys.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "ChoiceTableViewCell", for: indexPath)

        cell.textLabel?.text = data.allNames[indexPath.row]

        return cell
    }

}

请帮忙?:)

标签: iosswiftenums

解决方案


您当前实现的问题是您应该在方法中使用的allKeys和静态属性不是协议的一部分,因此您无法使用变量访问它们。allNamesUITableViewDataSourceTableViewSelectabledata

您应该修改您的协议以包含这些。

protocol TableViewSelectable {
    static var allKeys: [TableViewSelectable] {get}
    static var allNames: [String] {get}
}

您还需要相应地修改您enum的 s 以使其符合它。

enum Genders: Int, TableViewSelectable {
    case male = 0, female

    static let allKeys:[TableViewSelectable] = [male, female]
    static let allNames = [male.getName, female.getName]

    var getName: String {
        switch self {
        case .male:
            return "Male"
        case .female:
            return "Female"
        }
    }
}

enum Goals: Int, TableViewSelectable {
    case gainMuscleMass, getTrimFit, loseWeight

    static let allKeys:[TableViewSelectable] = [gainMuscleMass, getTrimFit, loseWeight]
    static let allNames = [gainMuscleMass.getName, getTrimFit.getName, loseWeight.getName]

    var getName: String {
        switch self {
        case .gainMuscleMass:
            return "Gain muscle mass"
        case .getTrimFit:
            return "Get trim & fit"
        case .loseWeight:
            return "Lose weight"
        }
    }
}

然后,您可以在数据源方法中简单地使用data.allKeysdata.allNames访问这些数组的相应元素。

class ChoiceListTableViewController: UITableViewController {
    var data:TableViewSelectable.Type! // make sure that this is actually initialized before accessing it

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.allKeys.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "id", for: indexPath)
        cell.textLabel?.text = data.allNames[indexPath.row]
        return cell
    }
}

推荐阅读