An example to use of generics with UITableViewController. With help of GenericListViewController.swift and BaseTableViewCell.swift, data can be populated without worrying about cell registration and TableView DataSource methods.
- Drag- Drop 'GenericListViewController.swift' and 'BaseTableViewCell.swift' file into your project folder.
- Create your data model say ModelRoot.
- Create a subclass of GenericListViewController say RootViewController.
- Create subclass of BaseTableViewCell say RootTableViewCell.
class RootTableViewCell: BaseTableViewCell<ModelRoot> {
fileprivate let padding_constant: CGFloat = 10.0
//Just to customize table cell and to explain use of custom cell
var labelName: UILabel!
override var item: ModelRoot! {
didSet {
self.labelName.text = item.name
}
}
}
class RootViewController: GenericListViewController<RootTableViewCell, ModelRoot> {
.......
}
class RootViewController: GenericListViewController<RootTableViewCell, ModelRoot> {
let dummyNames = [ModelRoot(name: "James"), ModelRoot(name: "Bianca"), ModelRoot(name: "Robert"), ModelRoot(name: "Peter"), ModelRoot(name: "Gagan"), ModelRoot(name: "Siree"), ModelRoot(name: "Sarah"), ModelRoot(name: "Pure"), ModelRoot(name: "Annu"), ModelRoot(name: "Const")]
//MARK:- View Controller life cyle
override func viewDidLoad() {
super.viewDidLoad()
//title
self.title = "Generic Table"
items = dummyNames
}
struct ModelRoot {
let name: String
}