So, my thought are:
I have different classes of UITableViewCell: CustomCell1, CustomCell2, CustomCell3
I have UITableView, and I need to add them through cellForRowAt, but there can be multiple cell of any type.
For example, table should contain this: CustomCell2, CustomCell2, CustomCell3, CustomCell1, CustomCell1, also there is Bool variable, and if it's true, them CustomCell1 should be added, if not then CustomCell3
Is there any way of creating array of this cell types? Like
arrayOfTypes = [CustomCell2, CustomCell2, CustomCell3, CustomCell1, CustomCell1, boolVariable ? CustomCell1 : CustomCell3]
So then in cellForRowAt I can call like this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let type = arrayOfTypes[indexPath.row]
switch type {
case is CustomCell1:
...
case is CustomCell2:
...
case is CustomCell3:
...
default:
...
}
}
Or maybe there's more convenient way of doing such a thing?
So now in project I do this:
enum CustomCellType {
case CustomCell1
case CustomCell2
case CustomCell3
}
class CellItem {
var cellType: CustomCellType
init(CellType type: CustomCellType) {
self.cellType = type
}
}
...
class WorkingWithData: UITableViewDataSource, UITableViewDelegate {
var tableData = [CellItem]()
override func viewDidLoad() {
fillCells()
}
func fillCells() {
let cellItem = CellItem(CellType: .CustomCell1)
tableData.append(cellItem)
let cellItem2 = CellItem(CellType: .CustomCell3)
tableData.append(cellItem2)
}
...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = tableData[indexPath.row]
switch item.cellType {
case .CustomCell1:
//dequeuereusablecell
case .CustomCell2:
//dequeuereusablecell
case .CustomCell3:
//dequeuereusablecell
default:
...
}
}
}