Skip to content

Commit

Permalink
Adds generic table vc & starts implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
fonkadelic committed Sep 25, 2017
1 parent e7b3ffc commit 9548545
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 52 deletions.
4 changes: 4 additions & 0 deletions UIKonf.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
5ABB02E11CDF825D00249A7A /* BasicTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5ABB02DF1CDF825D00249A7A /* BasicTableViewCell.swift */; };
5ABB02E21CDF825D00249A7A /* BasicTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 5ABB02E01CDF825D00249A7A /* BasicTableViewCell.xib */; };
5ABC7FAE1CE0633C001DFDA6 /* EventRegistrationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5ABC7FAD1CE0633C001DFDA6 /* EventRegistrationViewController.swift */; };
5AC1C9A71F77E85700DB66E9 /* TableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5AC1C9A61F77E85700DB66E9 /* TableViewController.swift */; };
7847DC687566618BAA3EFF9A /* Pods_UIKonfTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AB10D8B10C181FEA20BCA810 /* Pods_UIKonfTests.framework */; };
7AC566A48A661AC5A480A180 /* Pods_UIKonf.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EDB05EA2D1699C9E05DB7949 /* Pods_UIKonf.framework */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -95,6 +96,7 @@
5ABB02DF1CDF825D00249A7A /* BasicTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BasicTableViewCell.swift; sourceTree = "<group>"; };
5ABB02E01CDF825D00249A7A /* BasicTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = BasicTableViewCell.xib; sourceTree = "<group>"; };
5ABC7FAD1CE0633C001DFDA6 /* EventRegistrationViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EventRegistrationViewController.swift; sourceTree = "<group>"; };
5AC1C9A61F77E85700DB66E9 /* TableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewController.swift; sourceTree = "<group>"; };
AB10D8B10C181FEA20BCA810 /* Pods_UIKonfTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_UIKonfTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
AF13E69DF988FCC29E2DE180 /* Pods-UIKonf.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-UIKonf.debug.xcconfig"; path = "Pods/Target Support Files/Pods-UIKonf/Pods-UIKonf.debug.xcconfig"; sourceTree = "<group>"; };
EDB05EA2D1699C9E05DB7949 /* Pods_UIKonf.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_UIKonf.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -192,6 +194,7 @@
5A034DBB1F6EB5D900C83959 /* Constants */,
5A5448211CE5065A0008B531 /* Supporting Files */,
5AB72A1D1CDF5AD400C78B4E /* Assets.xcassets */,
5AC1C9A61F77E85700DB66E9 /* TableViewController.swift */,
5AB72A441CDF5B1100C78B4E /* SpeakerListViewController.swift */,
5A9943AB1CE88A3B003C3879 /* SpeakerDetailViewController.swift */,
5ABB02D51CDF602200249A7A /* EventListViewController.swift */,
Expand Down Expand Up @@ -512,6 +515,7 @@
files = (
5A54481B1CE503F90008B531 /* KWEBehaviour.m in Sources */,
5A9943AA1CE67A5E003C3879 /* KWEAdditions.m in Sources */,
5AC1C9A71F77E85700DB66E9 /* TableViewController.swift in Sources */,
5AB72A451CDF5B1100C78B4E /* SpeakerListViewController.swift in Sources */,
5A6117BF1CE0ED5E00A52AAA /* HeaderView.swift in Sources */,
5ABB02D61CDF602200249A7A /* EventListViewController.swift in Sources */,
Expand Down
20 changes: 10 additions & 10 deletions UIKonf/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12121" systemVersion="16G29" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="T4V-tt-YRd">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13196" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="T4V-tt-YRd">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12089"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13173"/>
<capability name="Aspect ratio constraints" minToolsVersion="5.1"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
Expand Down Expand Up @@ -37,7 +37,7 @@
<tabBarItem key="tabBarItem" title="Speakers" image="Megaphone" id="4fz-pa-uOn"/>
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" id="ZR6-BX-wsr">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<rect key="frame" x="0.0" y="20" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<nil name="viewControllers"/>
Expand All @@ -63,7 +63,7 @@
<rect key="frame" x="0.0" y="28" width="375" height="100"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="XIm-PH-Lr4" id="CiP-uS-89C">
<rect key="frame" x="0.0" y="0.0" width="375" height="100"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="99.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="GU1-DZ-JcH">
Expand All @@ -82,13 +82,13 @@
<rect key="frame" x="0.0" y="24" width="267" height="36"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="VZe-37-yus">
<rect key="frame" x="0.0" y="0.0" width="33" height="18"/>
<rect key="frame" x="0.0" y="0.0" width="32.5" height="18"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Description" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="3" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="eVl-LX-cRe">
<rect key="frame" x="0.0" y="20" width="69.5" height="16"/>
<rect key="frame" x="0.0" y="20" width="0.0" height="16"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
Expand Down Expand Up @@ -136,7 +136,7 @@
<rect key="frame" x="0.0" y="28" width="375" height="100"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="Keh-c7-Maz" id="bdC-o7-0IV">
<rect key="frame" x="0.0" y="0.0" width="375" height="100"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="99.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" spacing="8" translatesAutoresizingMaskIntoConstraints="NO" id="Xvo-CR-OKc">
Expand All @@ -155,13 +155,13 @@
<rect key="frame" x="0.0" y="24" width="267" height="36"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Title" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="oAM-7d-vGJ">
<rect key="frame" x="0.0" y="0.0" width="33" height="18"/>
<rect key="frame" x="0.0" y="0.0" width="32.5" height="18"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Description" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="3" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="wVX-Vi-sYJ">
<rect key="frame" x="0.0" y="20" width="69.5" height="16"/>
<rect key="frame" x="0.0" y="20" width="0.0" height="16"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
Expand Down Expand Up @@ -393,7 +393,7 @@
<tabBarItem key="tabBarItem" title="Events" image="Sign" id="ERc-EP-XIt"/>
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" id="H62-5f-Afi">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<rect key="frame" x="0.0" y="20" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<nil name="viewControllers"/>
Expand Down
58 changes: 16 additions & 42 deletions UIKonf/SpeakerListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,25 @@ import UIKit
import Reusable
import IBAnimatable

class SpeakerListViewController: UITableViewController {

fileprivate let speakers = Speaker.allSpeakers

override func viewDidLoad() {
super.viewDidLoad()
final class SpeakerListViewController: TableViewController<Speaker, BasicTableViewCell> {

tableView.register(cellType: BasicTableViewCell.self)
}

// MARK: - UITableViewDataSource

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return speakers.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(for: indexPath) as BasicTableViewCell
cell.accessoryType = .disclosureIndicator
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

let speaker = speakers[indexPath.row]
cell.configure(withTitle: speaker.name, detail: speaker.shortBio, image: speaker.image)

return cell
}

// MARK: - UITableViewDelegate

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
perform(segue: StoryboardSegue.Main.showSpeakerDetail)
}

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if let animatableCell = cell as? AnimatableTableViewCell {
animatableCell.animate(.none)
items = Speaker.allSpeakers
configureCell = { cell, speaker in
cell.accessoryType = .disclosureIndicator
cell.configure(withTitle: speaker.name, detail: speaker.shortBio, image: speaker.image)
}
}

// MARK: - Segue

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if case .showSpeakerDetail = StoryboardSegue.Main(rawValue: segue.identifier!)!,
let selectedIndexPath = tableView.indexPathForSelectedRow,
let speakerDetailViewController = segue.destination as? SpeakerDetailViewController {
speakerDetailViewController.speaker = speakers[selectedIndexPath.row]
willDisplayCell = { cell in
cell.animate(.none)
}
didSelect = { [unowned self] speaker in
self.perform(segue: StoryboardSegue.Main.showSpeakerDetail, prepare: { segue in
guard let detail = segue.destination as? SpeakerDetailViewController
else { fatalError("Wrong view controller type.") }
detail.speaker = speaker
})
}
}
}
68 changes: 68 additions & 0 deletions UIKonf/TableViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// TableViewController.swift
// UIKonf
//
// Copyright © 2017 Raised by Wolves. All rights reserved.
//

import UIKit
import Reusable

class TableViewController<Item, Cell: NibReusable>: UITableViewController {

var items: [Item] = [] {
didSet {
guard isViewLoaded else { return }
tableView.reloadData()
}
}

var configureCell: ((Cell, Item) -> ())?
var didSelect: ((Item) -> ())?
var willDisplayCell: ((Cell) -> ())?

override func viewDidLoad() {
super.viewDidLoad()

tableView.register(Cell.nib, forCellReuseIdentifier: Cell.reuseIdentifier)
}

// MARK: - UITableViewDataSource

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.reuseIdentifier, for: indexPath) as! Cell
configureCell?(cell, items[indexPath.row])

return cell as! UITableViewCell
}

// MARK: - UITableViewDelegate

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
willDisplayCell?(cell as! Cell)
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
didSelect?(items[indexPath.row])
}

// MARK: - Segues

private var prepareHandlers: [String: (UIStoryboardSegue) -> ()] = [:]

final func perform<S: SegueType>(segue: S, prepare: @escaping (UIStoryboardSegue) -> ()) where S.RawValue == String {
prepareHandlers[segue.rawValue] = prepare
performSegue(withIdentifier: segue.rawValue, sender: nil)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let identifier = segue.identifier,
let handler = prepareHandlers.removeValue(forKey: identifier) else { fatalError(); }

handler(segue)
}
}

0 comments on commit 9548545

Please sign in to comment.