Skip to content

Instantly share code, notes, and snippets.

@ChillyBwoy
Created March 14, 2019 21:39
Show Gist options
  • Save ChillyBwoy/a2c3442902ef5bbe32622822ac9acaaa to your computer and use it in GitHub Desktop.
Save ChillyBwoy/a2c3442902ef5bbe32622822ac9acaaa to your computer and use it in GitHub Desktop.
UIViewController with static table
//
// AddHabitViewController.swift
// every-habit
//
// Created by Eugene Cheltsov on 13/02/2019.
// Copyright © 2019 Eugene Cheltsov. All rights reserved.
//
import UIKit
final class ExampleViewController: StaticTableViewController {
override func setupUI() {
super.setupUI()
tableView.rowHeight = 44
}
override func cellsForSections() -> [StaticTableViewControllerSection] {
return [
("First Section", [
.cell(UITableViewCell()),
]),
("Second Section", [
.cell(UITableViewCell()),
]),
("Third Section", [
.cellWithHeight(UITableViewCell(), 42),
.cell(UITableViewCell()),
.cellWithHeight(UITableViewCell(), 100),
]),
]
}
}
import UIKit
enum StaticTableViewControllerCellContainer {
case cell(UITableViewCell)
case cellWithHeight(UITableViewCell, CGFloat)
}
typealias StaticTableViewControllerSection = (String, [StaticTableViewControllerCellContainer])
class StaticTableViewController: UIViewController {
private var cellsForSectionsData: [StaticTableViewControllerSection] = []
let tableView: UITableView = {
let view = UITableView(frame: .zero, style: .grouped)
view.allowsSelection = false
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
cellsForSectionsData = cellsForSections()
tableView.dataSource = self
tableView.delegate = self
setupUI()
}
func cellsForSections() -> [StaticTableViewControllerSection] {
return []
}
func setupUI() {
view.addSubview(tableView)
tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
}
extension StaticTableViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in _: UITableView) -> Int {
return cellsForSectionsData.count
}
func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
let (_, cells) = cellsForSectionsData[section]
return cells.count
}
func tableView(_: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let (_, cells) = cellsForSectionsData[indexPath.section]
let container = cells[indexPath.row]
switch container {
case let .cell(cell):
return cell
case .cellWithHeight(let cell, _):
return cell
}
}
func tableView(_: UITableView, titleForHeaderInSection section: Int) -> String? {
let (title, _) = cellsForSectionsData[section]
return title
}
func tableView(_: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let (_, cells) = cellsForSectionsData[indexPath.section]
let container = cells[indexPath.row]
switch container {
case .cell:
return tableView.rowHeight
case let .cellWithHeight(_, height):
return height
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment