ios - 作为 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
}
}
请帮忙?:)
解决方案
您当前实现的问题是您应该在方法中使用的allKeys
和静态属性不是协议的一部分,因此您无法使用变量访问它们。allNames
UITableViewDataSource
TableViewSelectable
data
您应该修改您的协议以包含这些。
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.allKeys
和data.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
}
}
推荐阅读
- r - 使用 R 脚本下载 MODIS 图像时出错
- java - Micronaut:在 Spock 中模拟工厂创建的 Bean
- macos - 在 shell 中并行化的最佳方法
- c# - 是否可以从 nameof(VALUE) 中获取 VALUE?
- sql - 如何仅获取记录的最大行数行?
- postgresql - 按日期范围分区 PostgreSQL 扫描所有分区
- c# - 如何在 SwaggerUI 中使用查询字符串传递对象数组
- c - openmp 中嵌套 for 循环的性能改进失败
- arrays - 类 stdClass 的对象无法在 Laravel 中转换为字符串?
- html - 如何在Angular Material stepper中用mat-chip替换下一个按钮?