Skip to content

Commit

Permalink
Update for Swift 3 GM
Browse files Browse the repository at this point in the history
  • Loading branch information
soffes committed Sep 19, 2016
1 parent d8d323b commit bf6a6ec
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 60 deletions.
4 changes: 2 additions & 2 deletions Example/CustomTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class CustomTableViewCell: UITableViewCell, Cell {
private lazy var centeredLabel: UILabel = {
let label = UILabel()
label.textAlignment = .center
label.textColor = .white()
label.textColor = .white
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
Expand All @@ -18,7 +18,7 @@ final class CustomTableViewCell: UITableViewCell, Cell {

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.backgroundColor = .gray()
contentView.backgroundColor = .gray

contentView.addSubview(centeredLabel)

Expand Down
2 changes: 1 addition & 1 deletion Example/NibTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class NibTableViewCell: UITableViewCell, Cell {
// MARK: - CellType

static func nib() -> UINib? {
return UINib(nibName: String(self), bundle: nil)
return UINib(nibName: String(describing: self), bundle: nil)
}

func configure(row: Row) {
Expand Down
10 changes: 5 additions & 5 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ViewController: TableViewController {

private let customAccessory: UIView = {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 32, height: 32))
view.backgroundColor = .red()
view.backgroundColor = .red
return view
}()

Expand All @@ -31,12 +31,12 @@ class ViewController: TableViewController {
dataSource.sections = [
Section(header: "Styles", rows: [
Row(text: "Value 1", detailText: "Detail", cellClass: Value1Cell.self),
Row(text: "Value 1", detailText: "with an image", cellClass: Value1Cell.self, image: UIImage(named: "Settings")),
Row(text: "Value 1", detailText: "with an image", image: UIImage(named: "Settings"), cellClass: Value1Cell.self),
Row(text: "Value 2", detailText: "Detail", cellClass: Value2Cell.self),
Row(text: "Subtitle", detailText: "Detail", cellClass: SubtitleCell.self),
Row(text: "Button", detailText: "Detail", cellClass: ButtonCell.self, selection: { [unowned self] in
Row(text: "Button", detailText: "Detail", selection: { [unowned self] in
self.showAlert(title: "Row Selection")
}),
}, cellClass: ButtonCell.self),
Row(text: "Custom from nib", cellClass: NibTableViewCell.self)
], footer: "This is a section footer."),
Section(header: "Accessories", rows: [
Expand All @@ -62,7 +62,7 @@ class ViewController: TableViewController {
]),
Section(header: "Editing", rows: [
Row(text: "Swipe this row", editActions: [
Row.EditAction(title: "Warn", backgroundColor: .orange(), selection: { [unowned self] in
Row.EditAction(title: "Warn", backgroundColor: .orange, selection: { [unowned self] in
self.showAlert(title: "Warned.")
}),
Row.EditAction(title: "Delete", style: .destructive, selection: { [unowned self] in
Expand Down
4 changes: 2 additions & 2 deletions Example/WindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import UIKit

@UIApplicationMain final class WindowController: UIResponder {
var window: UIWindow? = {
let window = UIWindow(frame: UIScreen.main().bounds)
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = UINavigationController(rootViewController: ViewController())
return window
}()
}


extension WindowController: UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
window?.makeKeyAndVisible()
return true
}
Expand Down
48 changes: 24 additions & 24 deletions Static/DataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ public class DataSource: NSObject {
}

didSet {
assert(Thread.isMainThread(), "You must access Static.DataSource from the main thread.")
assert(Thread.isMainThread, "You must access Static.DataSource from the main thread.")
updateTableView()
}
}

/// Sections to use in the table view.
public var sections: [Section] {
didSet {
assert(Thread.isMainThread(), "You must access Static.DataSource from the main thread.")
assert(Thread.isMainThread, "You must access Static.DataSource from the main thread.")
refresh()
}
}

/// Section index titles.
public var sectionIndexTitles: [String]? {
didSet {
assert(Thread.isMainThread(), "You must access Static.DataSource from the main thread.")
assert(Thread.isMainThread, "You must access Static.DataSource from the main thread.")
tableView?.reloadData()
}
}
Expand All @@ -50,7 +50,7 @@ public class DataSource: NSObject {

/// Initialize with optional `tableView` and `sections`.
public init(tableView: UITableView? = nil, sections: [Section]? = nil) {
assert(Thread.isMainThread(), "You must access Static.DataSource from the main thread.")
assert(Thread.isMainThread, "You must access Static.DataSource from the main thread.")

self.tableView = tableView
self.sections = sections ?? []
Expand All @@ -68,9 +68,9 @@ public class DataSource: NSObject {

// MARK: - Public

public func rowAtPoint(point: CGPoint) -> Row? {
public func row(atPoint point: CGPoint) -> Row? {
guard let indexPath = tableView?.indexPathForRow(at: point) else { return nil }
return rowForIndexPath(indexPath: indexPath)
return row(forIndexPath: indexPath)
}


Expand All @@ -88,7 +88,7 @@ public class DataSource: NSObject {
refreshRegisteredCells()
}

private func sectionForIndex(index: Int) -> Section? {
fileprivate func section(forIndex index: Int) -> Section? {
if sections.count <= index {
assert(false, "Invalid section index: \(index)")
return nil
Expand All @@ -97,8 +97,8 @@ public class DataSource: NSObject {
return sections[index]
}

private func rowForIndexPath(indexPath: NSIndexPath) -> Row? {
if let section = sectionForIndex(index: indexPath.section) {
fileprivate func row(forIndexPath indexPath: IndexPath) -> Row? {
if let section = section(forIndex: indexPath.section) {
let rows = section.rows
if rows.count >= indexPath.row {
return rows[indexPath.row]
Expand Down Expand Up @@ -174,11 +174,11 @@ public class DataSource: NSObject {

extension DataSource: UITableViewDataSource {
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sectionForIndex(index: section)?.rows.count ?? 0
return self.section(forIndex: section)?.rows.count ?? 0
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let row = rowForIndexPath(indexPath: indexPath) {
if let row = self.row(forIndexPath: indexPath) {
let tableCell = tableView.dequeueReusableCell(withIdentifier: row.cellIdentifier, for: indexPath)

if let cell = tableCell as? Cell {
Expand All @@ -196,36 +196,36 @@ extension DataSource: UITableViewDataSource {
}

public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionForIndex(index: section)?.header?._title
return self.section(forIndex: section)?.header?._title
}

public func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return sectionForIndex(index: section)?.header?._view
return self.section(forIndex: section)?.header?._view
}

public func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return sectionForIndex(index: section)?.header?.viewHeight ?? UITableViewAutomaticDimension
return self.section(forIndex: section)?.header?.viewHeight ?? UITableViewAutomaticDimension
}

public func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return sectionForIndex(index: section)?.footer?._title
return self.section(forIndex: section)?.footer?._title
}

public func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return sectionForIndex(index: section)?.footer?._view
return self.section(forIndex: section)?.footer?._view
}

public func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return sectionForIndex(index: section)?.footer?.viewHeight ?? UITableViewAutomaticDimension
return self.section(forIndex: section)?.footer?.viewHeight ?? UITableViewAutomaticDimension
}

public func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return rowForIndexPath(indexPath: indexPath)?.canEdit ?? false
return row(forIndexPath: indexPath)?.canEdit ?? false
}

@objc(tableView:editActionsForRowAtIndexPath:)
public func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
return rowForIndexPath(indexPath: indexPath)?.editActions.map {
return row(forIndexPath: indexPath)?.editActions.map {
action in
let rowAction = UITableViewRowAction(style: action.style, title: action.title) { (_, _) in
action.selection?()
Expand All @@ -247,13 +247,13 @@ extension DataSource: UITableViewDataSource {
}

public func sectionIndexTitles(for tableView: UITableView) -> [String]? {
guard let sectionIndexTitles = sectionIndexTitles where sectionIndexTitles.count >= sections.count else { return nil }
guard let sectionIndexTitles = sectionIndexTitles, sectionIndexTitles.count >= sections.count else { return nil }
return sectionIndexTitles
}

public func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
for (i, section) in sections.enumerated() {
if let indexTitle = section.indexTitle where indexTitle == title {
if let indexTitle = section.indexTitle, indexTitle == title {
return i
}
}
Expand All @@ -264,21 +264,21 @@ extension DataSource: UITableViewDataSource {

extension DataSource: UITableViewDelegate {
public func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
return rowForIndexPath(indexPath: indexPath)?.isSelectable ?? false
return row(forIndexPath: indexPath)?.isSelectable ?? false
}

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if automaticallyDeselectRows {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
}

if let row = rowForIndexPath(indexPath: indexPath) {
if let row = row(forIndexPath: indexPath) {
row.selection?()
}
}

public func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
if let row = rowForIndexPath(indexPath: indexPath) {
if let row = row(forIndexPath: indexPath) {
row.accessory.selection?()
}
}
Expand Down
14 changes: 7 additions & 7 deletions Static/Row.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,27 @@ public struct Row: Hashable, Equatable {
/// Table view cell accessory type
public var type: UITableViewCellAccessoryType {
switch self {
case disclosureIndicator: return .disclosureIndicator
case detailDisclosureButton(_): return .detailDisclosureButton
case checkmark: return .checkmark
case detailButton(_): return .detailButton
case .disclosureIndicator: return .disclosureIndicator
case .detailDisclosureButton(_): return .detailDisclosureButton
case .checkmark: return .checkmark
case .detailButton(_): return .detailButton
default: return .none
}
}

/// Accessory view
public var view: UIView? {
switch self {
case view(let view): return view
case .view(let view): return view
default: return nil
}
}

/// Selection block for accessory buttons
public var selection: Selection? {
switch self {
case detailDisclosureButton(let selection): return selection
case detailButton(let selection): return selection
case .detailDisclosureButton(let selection): return selection
case .detailButton(let selection): return selection
default: return nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion Static/Section.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public struct Section: Hashable, Equatable {
}


extension Section.Extremity: StringLiteralConvertible {
extension Section.Extremity: ExpressibleByStringLiteral {
public typealias UnicodeScalarLiteralType = StringLiteralType
public typealias ExtendedGraphemeClusterLiteralType = StringLiteralType

Expand Down
26 changes: 13 additions & 13 deletions Static/TableViewController.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import UIKit

/// Table view controller with a `DataSource` setup to use its `tableView`.
public class TableViewController: UIViewController {
open class TableViewController: UIViewController {

// MARK: - Properties

/// Returns the table view managed by the controller object.
public let tableView: UITableView
open let tableView: UITableView

/// A Boolean value indicating if the controller clears the selection when the table appears.
///
/// The default value of this property is true. When true, the table view controller clears the table’s current selection when it receives a viewWillAppear: message. Setting this property to false preserves the selection.
public var clearsSelectionOnViewWillAppear: Bool = true
open var clearsSelectionOnViewWillAppear: Bool = true

/// Table view data source.
public var dataSource = DataSource() {
open var dataSource = DataSource() {
willSet {
dataSource.tableView = nil
}
Expand Down Expand Up @@ -50,23 +50,23 @@ public class TableViewController: UIViewController {

// MARK: - UIViewController

public override func loadView() {
open override func loadView() {
tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view = tableView
}

public override func viewWillAppear(_ animated: Bool) {
open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
performInitialLoad()
clearSelectionsIfNecessary(animated: animated)
}

public override func viewDidAppear(_ animated: Bool) {
open override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
tableView.flashScrollIndicators()
}

public override func setEditing(_ editing: Bool, animated: Bool) {
open override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
tableView.setEditing(editing, animated: animated)
}
Expand All @@ -82,8 +82,8 @@ public class TableViewController: UIViewController {
}

private func clearSelectionsIfNecessary(animated: Bool) {
guard let selectedIndexPaths = tableView.indexPathsForSelectedRows where clearsSelectionOnViewWillAppear else { return }
guard let coordinator = transitionCoordinator() else {
guard let selectedIndexPaths = tableView.indexPathsForSelectedRows, clearsSelectionOnViewWillAppear else { return }
guard let coordinator = transitionCoordinator else {
deselectRowsAtIndexPaths(indexPaths: selectedIndexPaths, animated: animated)
return
}
Expand All @@ -93,19 +93,19 @@ public class TableViewController: UIViewController {
}

let completion: (UIViewControllerTransitionCoordinatorContext) -> Void = { [weak self] context in
if context.isCancelled() {
if context.isCancelled {
self?.selectRowsAtIndexPaths(indexPaths: selectedIndexPaths, animated: animated)
}
}

coordinator.animate(alongsideTransition: animation, completion: completion)
}

private func selectRowsAtIndexPaths(indexPaths: [NSIndexPath], animated: Bool) {
private func selectRowsAtIndexPaths(indexPaths: [IndexPath], animated: Bool) {
indexPaths.forEach { tableView.selectRow(at: $0 as IndexPath, animated: animated, scrollPosition: .none) }
}

private func deselectRowsAtIndexPaths(indexPaths: [NSIndexPath], animated: Bool) {
private func deselectRowsAtIndexPaths(indexPaths: [IndexPath], animated: Bool) {
indexPaths.forEach { tableView.deselectRow(at: $0 as IndexPath, animated: animated) }
}
}
Loading

0 comments on commit bf6a6ec

Please sign in to comment.