I’ve a wide range of kinds of cells to show in my UITableView
. I generate the reuseIdentifier
for the cells based mostly upon whether or not properties like title, physique, metadata, favicon and so forth are true or false. So, every property can have two states: true or false.
struct ReuseIdentifier : Codable, CustomStringConvertible {
var description : String
let sectionTitle : Bool
let title : Bool
let physique : Bool
let metadata : Bool
let favicon : Bool
let onCommentsPage : Bool
let indent : Bool
let collapsed : Bool
static var allPermutations : [ReuseIdentifier]
}
extension String {
var decodedReuseIdentifier : ReuseIdentifier? {
guard let information = information(utilizing: .utf8), let d = strive? JSONDecoder().decode(ReuseIdentifier.self, from: information) else {
return nil
}
return d
}
}
In my customized cell’s init, I decode the reuseIdentifier
String to the corresponding ReuseIdentifier
:
class Cell: UITableViewCell {
non-public var slidingView = UIView()
var title : UILabel?
var physique : UILabel?
//....relaxation
override init(model: UITableViewCell.CellStyle, reuseIdentifier: String?) {
tremendous.init(model: model, reuseIdentifier: reuseIdentifier)
guard let decodedReuseIdentifier = reuseIdentifier?.decodedReuseIdentifier else {
print("Did not decodedReuseIdentifier")
return
}
//Create every view based mostly upon whether or not decodedReuseIdentifier's variable is true or false
}
}
I take advantage of my ReuseIdentifier
‘s allPermutations
to register the corresponding reusable cell:
ReuseIdentifier.allPermutations.forEach { id in
tableView.register(Cell.self, forCellReuseIdentifier: id.description)
}
Then in my cellForRowAt
operate, I create the ReuseIdentifier
and deque a cell with it is String description:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
Is there a extra common approach for my allPermutations
operate? A way the place I haven’t got to specify every property identify and indexes? As you discover, I’m at present having to hardcode all of the property names in addition to the 8
and indexes.
Alternatively, is there a greater approach to do that?