Skip to content

Instantly share code, notes, and snippets.

View pranjalsatija's full-sized avatar

Pranjal Satija pranjalsatija

View GitHub Profile
@pranjalsatija
pranjalsatija / ReusableTableViewCell.swift
Created February 8, 2020 00:31
A reusable table view cell. Storyboard not included.
import UIKit
class ReusableTableViewCell: UITableViewCell {
static let identifier = String(describing: ReusableTableViewCell.self)
var leading: UIView? {
didSet {
if let oldValue = oldValue {
stackView.removeArrangedSubview(oldValue)
}
@pranjalsatija
pranjalsatija / UITableViewExtension.swift
Created February 8, 2020 00:07
An extension on UITableView to add support for table view descriptors.
extension UITableView {
static private let snapshotKey = "snapshot"
var descriptor: UITableViewDescriptor? {
get { objc_getAssociatedObject(self, Self.snapshotKey) as? UITableViewDescriptor }
set {
objc_setAssociatedObject(self, Self.snapshotKey, newValue, .OBJC_ASSOCIATION_ASSIGN)
delegate = newValue
newValue?.tableView = self
@pranjalsatija
pranjalsatija / UITableViewDescriptor Example.swift
Last active February 8, 2020 00:20
A small example showing off how to use UITableViewDescriptor.
tableView.descriptor = UITableViewDescriptor(sections: [
UITableViewSection(
data: $data, // data: [Int], $data: Publisher<Int, Never>
cellProvider: {(tableView, indexPath, element) in
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! Cell
cell.titleLabel.text = "Item #\(element)"
return cell
}
)
])
@pranjalsatija
pranjalsatija / IdentifiableCell.swift
Last active March 29, 2020 07:59
An example of how to make dequeuing and instantiating UITableViewCells a little nicer.
protocol IdentifiableCell {
static var identifier: String { get }
}
extension IdentifiableCell {
static var identifier: String { String(describing: Self.self) }
}
extension IdentifiableCell where Self: UITableViewCell {
static func dequeue(in tableView: UITableView) -> Self {
@pranjalsatija
pranjalsatija / UITableViewDescriptor.swift
Last active December 31, 2020 00:06
A simple implementation of UITableViewDescriptor.
class UITableViewDescriptor: NSObject {
var tableView: UITableView? {
didSet {
configureBindings()
}
}
private var dataSource: UITableViewDiffableDataSource<Int, AnyHashable>!
private var sections = [UITableViewSection]()
private let sectionsPublisher: AnyPublisher<[UITableViewSection], Never>
@pranjalsatija
pranjalsatija / UITableViewDiffableDataSource.swift
Last active February 8, 2020 00:15
A simple example that shows how to use UITableViewDiffableDataSource.
typealias SectionType: Hashable // The type you're using to identify sections.
typealias DataType: Hashable // The type of the data you're displaying.
let dataSource = UITableViewDiffableDataSource<SectionType, DataType>(
tableView: tableView,
cellProvider: {(tableView, indexPath, data) in
// construct and return a cell here
}
)
@pranjalsatija
pranjalsatija / UITableViewSection.swift
Last active February 8, 2020 00:22
A simple implementation for UITableViewSection.
class UITableViewSection: NSObject {
private(set) var items = [AnyHashable]()
private(set) var sectionIndex: Int!
private let itemsPublisher: AnyPublisher<[AnyHashable], Never>
private let cellProvider: (UITableView, IndexPath, AnyHashable) -> UITableViewCell
private var subscriptions = Set<AnyCancellable>()
convenience init<T: Hashable>(
@pranjalsatija
pranjalsatija / UITableView Abstraction.swift
Last active February 8, 2020 00:20
A theoretical example of a "perfect" abstraction over UITableViewDataSource and UITableViewDelegate.
tableView.descriptor = UITableViewDescriptor(sections: [
UITableViewSection(
items: $data,
cellProvider: {(tableView, indexPath, element) in
let cell = Cell.dequeue(in: tableView)
cell.configure(with: element)
return cell
}
)
@pranjalsatija
pranjalsatija / Static Example.swift
Created February 7, 2020 20:30
A simple example of using Static to populate a table view.
let dataSource = DataSource()
dataSource.sections = [
Section(rows: [
Row(text: "Hello")
])
]
dataSource.tableView = tableView
@pranjalsatija
pranjalsatija / UITableViewCoordinator.swift
Created February 7, 2020 18:41
A simple example of conformance to UITableViewDataSource and UITableViewDelegate.
class UITableViewCoordinator: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "identifier") as! Cell
// configure it
return cell
}