A wrapper for UI elements that dequeue cells. Cells are dequeued by their corresponding type.
This is a full implementation that assumes you are not using interface building.
import UIKit
import Dequeue
final class TableViewCell: UITableViewCell, DequeableComponentIdentifiable {}
final class TableView: UITableView, DequeableTableView {}
final class ViewController: UIViewController, UITableViewDataSource {
private let tableView = TableView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.register(cellType: TableViewCell.self, hasXib: false)
tableView.dataSource = self
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.frame = view.bounds
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let tableView = tableView as? DequeableTableView else {
fatalError("Ooops. Missed a step.")
}
return cell(in: tableView, at: indexPath)
}
private func cell(in tableView: DequeableTableView, at indexPath: IndexPath) -> UITableViewCell {
let cell: TableViewCell = tableView.dequeueCell(at: indexPath)
cell.textLabel?.text = "Row \(indexPath.row + 1)"
return cell
}
}
If you are using interface builder, then these addtiional implementation steps are required.
ID
. For example MyTableViewCellID
..xib
then you will need to pass true here.tableView.register(cellType: TableViewCell.self, hasXib: true)
N.B. You do not need to register if you are using a storyboard.
link |
Stars: 0 |
Last commit: 2 years ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics