【发布时间】:2019-05-19 22:57:03
【问题描述】:
说明
我有与类名对应的案例名称的枚举。我使用枚举来填充 UITableView 的部分/行。当用户选择一行时,我想实例化相应的类并实例化该类的一个对象。
// example enum:
enum BodiceEnum: String, CaseIterable {
case AddBraCups
case AddOrRemoveBoning
// other cases hidden
}
// example classes:
class AddBraCups { // implementation hidden }
class AddOrRemoveBoning { // implementation hidden}
附加上下文
我制作了一个查找表,将“Section”枚举案例连接到其对应的详细枚举案例:
var alterationsLookupTable: [(String,[Any])] = [
("Bodice",BodiceEnum.allCases),
("Neckline",NecklineEnum.allCases),
("Sides",SidesEnum.allCases),
("Sleeves or Straps",SleevesOrStrapsEnum.allCases),
("Back of Dress",BackOfDressEnum.allCases),
("Seams",SeamsEnum.allCases),
("Hem",HemEnum.allCases),
("Skirt",SkirtEnum.allCases),
("Veils",VeilsEnum.allCases),
("Prom - Straps",PromStrapsEnum.allCases),
("Prom - Take in/out",PromTakeInOrOutEnum.allCases),
("Prom - Hem",PromHemEnum.allCases),
("Tux",TuxEnum.allCases),
]
当前的 UITableView 部分对应于这个 alterationsLookupTable 数组中的一个索引。
一旦获得正确的部分类型,我就会切换该部分的相应枚举案例。我正在切换 itemsTuple.0,然后使用当前 indexPath.row 值作为 itemsTuple.1 中的索引
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
//var alterationDetailsArray: [String]()
let section = indexPath.section
var cellText: String = ""
let itemTuple = alterationsLookupTable[section]
switch itemTuple.0 {
case "Bodice":
let items = itemTuple.1 as! [BodiceEnum]
cellText = items[indexPath.row].readable
case "Neckline":
let items = itemTuple.1 as! [NecklineEnum]
cellText = items[indexPath.row].readable
case "Sides":
let items = itemTuple.1 as! [SidesEnum]
cellText = items[indexPath.row].readable
case "Sleeves or Straps":
let items = itemTuple.1 as! [SleevesOrStrapsEnum]
cellText = items[indexPath.row].readable
case "Back of Dress":
let items = itemTuple.1 as! [BackOfDressEnum]
cellText = items[indexPath.row].readable
case "Seams":
let items = itemTuple.1 as! [SeamsEnum]
cellText = items[indexPath.row].readable
case "Hem":
let items = itemTuple.1 as! [HemEnum]
cellText = items[indexPath.row].readable
case "Skirt":
let items = itemTuple.1 as! [SkirtEnum]
cellText = items[indexPath.row].readable
case "Veils":
let items = itemTuple.1 as! [VeilsEnum]
cellText = items[indexPath.row].readable
case "Prom - Straps":
let items = itemTuple.1 as! [PromStrapsEnum]
cellText = items[indexPath.row].readable
case "Prom - Take in/out":
let items = itemTuple.1 as! [PromTakeInOrOutEnum]
cellText = items[indexPath.row].readable
case "Prom - Hem":
let items = itemTuple.1 as! [PromHemEnum]
cellText = items[indexPath.row].readable
case "Tux":
let items = itemTuple.1 as! [TuxEnum]
cellText = items[indexPath.row].readable
default:
cellText = "not valid cell text"
}
cell.textLabel?.text = cellText
return cell
}
}
我一直在查看this,但我似乎无法让它发挥作用。我对这个问题的(一点)理解是 Swift 的类型安全。我猜有 Swifty(惯用的)方法可以实现这一点。
更新 2:
这是一个 Alteration Section 的示例 —> Hem 及其子类之一 —> AddHemLace
class Hem : Codable {
var minCost: Float
var maxCost: Float
var actualCost: Float
var name: String {
let thisType = type(of: self)
return String(describing: thisType)
}
init(minCost: Float, maxCost: Float, actualCost: Float) {
self.minCost = minCost
self.maxCost = maxCost
self.actualCost = actualCost
}
convenience init() {
self.init(minCost: -1, maxCost: -1, actualCost: -1)
}
}
class AddHemLace : Hem {
var costDetails: String?
var costUnit: String?
var units: Int = 1
var secondaryCost: Float = 0.0
var secondaryCostDetails: String?
var totalCost : Float {
return self.actualCost * Float(self.units) + self.secondaryCost
}
init() {
let min: Float = 50.00
let max: Float = 80.00
let actual: Float = min
super.init(minCost: min, maxCost: max, actualCost: actual)
}
required init(from decoder: Decoder) throws {
fatalError("init(from:) has not been implemented")
}
}
问题:我不知道如何填充 UITableView
我创建了与我的类具有平行结构的相关枚举。
enum AlterationSectionsEnum: String, CaseIterable {
case Hem
// other cases
}
enum HemEnum: String, CaseIterable {
case cutAndReplaceHem
// other cases
}
然后我使用了一个大的 switch 语句、一个查找表和一些字符串解析 foo 来填充 UITableView。 Lots of code smell.
问题
当我试图理解这个 answer 时,我发现现在可以直接使用我的类来填充 UITableView。
我不知道该从何说起:
“例如,它应该提供一种方法来填充 NSView 或表格单元格。这样,每个类都可以定义自己的 UI 类型来呈现其特定的可配置参数……”
【问题讨论】:
-
当用户选择一行时,我想实例化对应的类并实例化该类的一个对象。我不认为为此,您需要枚举。请提供更多背景信息。
-
为什么首先有一个枚举? IE。枚举案例有什么用途,而类本身无法实现?
-
我对帖子进行了编辑
-
感谢更新。但是我看不到你试图在哪里实例化你的类,以及你想如何使用它们。
readable是什么? -
我尝试使用
NSClassFromString(className)实例化我的类,其中className是所选枚举的 rawValue。我试过这个:stackoverflow.com/questions/24030814/…
标签: swift uitableview class enums