Skip to content

Instantly share code, notes, and snippets.

@damodarnamala
Last active April 23, 2023 06:05
Show Gist options
  • Save damodarnamala/754a79d248174cdc54a8ab7a5821d4c4 to your computer and use it in GitHub Desktop.
Save damodarnamala/754a79d248174cdc54a8ab7a5821d4c4 to your computer and use it in GitHub Desktop.
Generic Tableview
//
// ViewController.swift
// ReusableTableView
//
// Created by Damodar Namala on 23/04/23.
//
import UIKit
class ViewController: UIViewController {
let viewController = PersonTableViewController()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func next() {
viewController.load(items: [Person(name: "Damoda", age: 36)]) { person, cell in
cell.textLabel?.text = person.name
}
viewController.didSelectRowAt = { item in
print(item.name)
}
self.navigationController?.pushViewController(viewController, animated: true)
}
}
class GenericTableViewController<T, Cell: UITableViewCell>: UITableViewController {
var items: [T] = []
var configureCell: (T, Cell) -> Void
var didSelectRowAt: ((T) -> Void)?
init(items: [T] = [], configureCell: @escaping (T, Cell) -> Void) {
self.items = items
self.configureCell = configureCell
super.init(style: .plain)
self.tableView.register(Cell.self, forCellReuseIdentifier: "Cell")
}
func load(items: [T], configureCell: @escaping (T, Cell) -> Void) {
self.items = items
self.configureCell = configureCell
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("\(#function) has not been implemented")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Cell
let item = items[indexPath.row]
configureCell(item, cell)
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = items[indexPath.row]
didSelectRowAt?(item)
}
}
struct Person {
let name: String
let age: Int
}
class PersonCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
// Configure the cell here
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class PersonTableViewController: GenericTableViewController<Person, PersonCell> {
init() {
super.init() { person, cell in
cell.textLabel?.text = person.name
cell.detailTextLabel?.text = "\(person.age)"
}
}
func load(persons: [Person], configureCell: @escaping (Person, PersonCell) -> Void) {
self.items = items
self.configureCell = configureCell
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("\(#function) has not been implemented")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment