Skip to content

Instantly share code, notes, and snippets.

@FahimF
Last active October 19, 2020 14:04
Show Gist options
  • Save FahimF/196d28f18559e449da93d5a65f88b632 to your computer and use it in GitHub Desktop.
Save FahimF/196d28f18559e449da93d5a65f88b632 to your computer and use it in GitHub Desktop.
A UITableView implementation of UIListContentConfiguration
import UIKit
class SimpleTableView: UIViewController, UITableViewDelegate, UITableViewDataSource {
private var tableView: UITableView!
private let reuseIdentifier = "BookCell"
override func viewDidLoad() {
super.viewDidLoad()
// View title
title = "Books - Table"
// Add table view
tableView = UITableView(frame: view.bounds, style: .insetGrouped)
tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(tableView)
tableView.delegate = self
tableView.dataSource = self
// Register cell
tableView.register(UITableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
}
// MARK: - Table View Data Source / Delegate
func numberOfSections(in tableView: UITableView) -> Int {
return Book.sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Book.booksFor(section: section).count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: self.reuseIdentifier, for: indexPath)
var content = cell.defaultContentConfiguration()
let section = Book.sections[indexPath.section]
if let arr = Book.books[section] {
let book = arr[indexPath.row]
content.image = book.authType == .single ? UIImage(systemName: "person.fill") : UIImage(systemName: "person.2.fill")
content.text = book.title
content.secondaryText = book.author
}
cell.contentConfiguration = content
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment