Skip to content

Instantly share code, notes, and snippets.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let (value, reusableId) = self.values[indexPath.section].rows[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: reusableId, for: indexPath)
switch (cell, value) {
case (_ as ProfileTableViewCell, _ as Void): break
case let (cell as TaskTableViewCell, value as Task):
cell.configure(with: value)
default: break
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return values[section].headerTitle
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return values[section].rows.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return values.count
}
class CommonizedDataSource: NSObject, UITableViewDataSource {
private var values: [(headerTitle: String?, rows: [(value: Any, reusableId: String)])] = []
func load(_ tasks: [Task]) {
self.values = [
(
headerTitle: nil,
rows: [(value: (), reusableId: "ProfileTableViewCell")]
),
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch section {
case 0:
return nil
case 1:
return Category.personal.rawValue
case 2:
return Category.shopping.rawValue
case 3:
return Category.work.rawValue
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
return tableView.dequeueReusableCell(withIdentifier: "ProfileTableViewCell") as! ProfileTableViewCell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: "TaskTableViewCell") as! TaskTableViewCell
let personalTask = tasks.filter { $0.category == .personal }[indexPath.row]
cell.configure(with: personalTask)
return cell
case 2:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return 1
case 1:
return tasks.filter { $0.category == .personal }.count
case 2:
return tasks.filter { $0.category == .shopping }.count
case 3:
return tasks.filter { $0.category == .work }.count
func numberOfSections(in tableView: UITableView) -> Int {
return Category.allCases.count + 1
}
class SimpleDataSource: NSObject, UITableViewDataSource {
private var tasks: [Task] = []
func load(with tasks: [Task]) {
self.tasks = tasks
}
.
.
.