Code: Select all
struct ReuseIdentifier : Codable, CustomStringConvertible {
var description : String {
let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys
guard let data = try? encoder.encode(self), let str = String(data: data, encoding: .utf8) else {
return ""
}
return str
}
let sectionTitle : Bool
let title : Bool
let body : Bool
let metadata : Bool
let favicon : Bool
let onCommentsPage : Bool
let indent : Bool
let collapsed : Bool
static var allPermutations : [ReuseIdentifier] {
var result = [ReuseIdentifier]()
for i in 0.. 0) & 1 == 1,// Notice how all these needs to be hardcoded
title: (i >> 1) & 1 == 1,
body: (i >> 2) & 1 == 1,
metadata: (i >> 3) & 1 == 1,
favicon: (i >> 4) & 1 == 1,
onCommentsPage: (i >> 5) & 1 == 1,
indent: (i >> 6) & 1 == 1,
collapsed: (i >> 7) & 1 == 1
)
)
}
return result
}
}
extension String {
var decodedReuseIdentifier : ReuseIdentifier? {
guard let data = data(using: .utf8), let d = try? JSONDecoder().decode(ReuseIdentifier.self, from: data) else {
return nil
}
return d
}
}
Code: Select all
class Cell: UITableViewCell {
private var slidingView = UIView()
var title : UILabel?
var body : UILabel?
//....rest
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
guard let decodedReuseIdentifier = reuseIdentifier?.decodedReuseIdentifier else {
print("Failed to decodedReuseIdentifier")
return
}
//Create each view based upon whether decodedReuseIdentifier's variable is true or false
}
}
Code: Select all
ReuseIdentifier.allPermutations.forEach { id in
tableView.register(Cell.self, forCellReuseIdentifier: id.description)
}
Code: Select all
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let record = records[indexPath.row]
let reuseIdentifier = ReuseIdentifier(sectionTitle: record.sectionTitle != nil, title: record.title != nil, body: record.body != nil, metadata: record.domain != nil || record.author != nil, favicon: record.domain != nil, onCommentsPage: isCommentsPage, indent: record.indent ?? 0 > 0, collapsed: false)
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier.description, for: indexPath) as! Cell
//....rest
}