我有一个需要处理9种不同状态的表格视图。 我有两个部分包含不同的数据模型(A,B) 这两个部分的逻辑相互依赖
0,1
是否存在处理多个状态的标准?我正在考虑创建某种状态结构。处理此问题的“行业标准”是什么?
这是我目前正在使用的-许多计算变量
1) !A.isEmpty
2) !B.isEmpty
3) A show expandable footer (A.count > 5)
4) B show expandable footer (B.count > 5)
5) A shows collapsable footer (user clicked on A.expandable footer)
6) B shows collapsable footer (user clicked on B.expandable footer
7) A.count > 3 && B.isEmpty
8) B.count > 3 && A.isEmpty
9) A.count > 3 && B.isEmpty && A shows collapsable footer (user clicked on A.expandable footer)
答案 0 :(得分:0)
我认为枚举或结构可以满足您的需求。
struct Video {
enum State {
case willDownload(from: URL)
case downloading(task: Task)
case playing(file: File, progress: Double)
case paused(file: File, progress: Double)
}
var state: State
}
private func resolveActionButtonImage() -> UIImage {
// The image for the action button is declaratively resolved
// directly from the video state
switch video.state {
// We can easily discard associated values that we don't need
// by simply omitting them
case .willDownload:
return .wait
case .downloading:
return .cancel
case .playing:
return .pause
case .paused:
return .play
}
}
}
参考: https://www.swiftbysundell.com/posts/modelling-state-in-swift
答案 1 :(得分:0)
我决定使用一个对象来保存逻辑并使用位掩码充当状态。
private struct StateModel: OptionSet {
let rawValue: Int
static let AIsEmpty = StateModel(rawValue: 1 << 0)
static let AHasValues = StateModel(rawValue: 1 << 1)
static let BIsEmpty = StateModel(rawValue: 1 << 2)
static let BHasValues = StateModel(rawValue: 1 << 3)
static let AFooterIsVisible = StateModel(rawValue: 1 << 4)
static let BIsVisible = StateModel(rawValue: 1 << 5)
static let AFooterIsExpanded = StateModel(rawValue: 1 << 6)
static let BFooterIsExpanded = StateModel(rawValue: 1 << 7)
static let AFooterShowsCTA: StateModel = [.AHasValues, .BIsEmpty]
static let BFooterShowCTA: StateModel = [.AIsEmpty, .BHasValues]
static let ACTAAsCell: StateModel = [.AFooterShowsCTA, .AFooterIsVisible]
static let BCTAAsCell: StateModel = [.BFooterShowCTA, .BFooterIsExpanded]
}
class Logic {
var stateModel: StateModel = []
//Should AFooter show CTA?
var showAFooter: Bool {
stateModel.contains(.AFooterShowsCTA)
}
}