Skip to content

Commit

Permalink
Merge pull request #79 from maxsokolov/bugfixes-and-improvements
Browse files Browse the repository at this point in the history
Bugfixes and improvements
  • Loading branch information
maxsokolov authored Mar 17, 2018
2 parents d8ae329 + de5593f commit f05511c
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 92 deletions.
10 changes: 1 addition & 9 deletions Demo/Classes/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
//
// AppDelegate.swift
// TabletDemo
//
// Created by Max Sokolov on 08/11/15.
// Copyright © 2015 Tablet. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//
// AutolayoutCellsController.swift
// TableKitDemo
//
// Created by Max Sokolov on 18/06/16.
// Copyright © 2016 Tablet. All rights reserved.
//

import UIKit
import TableKit

Expand Down Expand Up @@ -56,6 +48,15 @@ class AutolayoutCellsController: UIViewController {
}

tableDirector += section

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Clear", style: .done, target: self, action: #selector(clear))
}

@objc
func clear() {

tableDirector.clear()
tableDirector.reload()
}

func getViewHeight(view: UIView, width: CGFloat) -> CGFloat {
Expand Down
8 changes: 0 additions & 8 deletions Demo/Classes/Presentation/Controllers/MainController.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//
// MainController.swift
// TabletDemo
//
// Created by Max Sokolov on 16/04/16.
// Copyright © 2016 Tablet. All rights reserved.
//

import UIKit
import TableKit

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//
// NibCellsController.swift
// TableKitDemo
//
// Created by Max Sokolov on 18/06/16.
// Copyright © 2016 Tablet. All rights reserved.
//

import UIKit
import TableKit

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//
// AutolayoutSectionHeaderView.swift
// TableKitDemo
//
// Created by Max on 13/12/2017.
// Copyright © 2017 Tablet. All rights reserved.
//

import UIKit

final class AutolayoutSectionHeaderView: UIView {
Expand Down
8 changes: 0 additions & 8 deletions Demo/Classes/Presentation/Views/AutolayoutTableViewCell.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//
// AutolayoutTableViewCell.swift
// TabletDemo
//
// Created by Max Sokolov on 24/05/16.
// Copyright © 2016 Tablet. All rights reserved.
//

import UIKit
import TableKit

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//
// ConfigurableTableViewCell.swift
// TableKitDemo
//
// Created by Max Sokolov on 18/06/16.
// Copyright © 2016 Tablet. All rights reserved.
//

import UIKit
import TableKit

Expand Down
10 changes: 1 addition & 9 deletions Demo/Classes/Presentation/Views/NibTableViewCell.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//
// NibTableViewCell.swift
// TableKitDemo
//
// Created by Max Sokolov on 18/06/16.
// Copyright © 2016 Tablet. All rights reserved.
//

import UIKit
import TableKit

Expand All @@ -20,4 +12,4 @@ class NibTableViewCell: UITableViewCell, ConfigurableCell {
func configure(with number: Int) {
titleLabel.text = "\(number)"
}
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<a href="https://travis-ci.org/maxsokolov/TableKit"><img src="https://api.travis-ci.org/maxsokolov/TableKit.svg" alt="Build Status" /></a>
<a href="https://developer.apple.com/swift"><img src="https://img.shields.io/badge/Swift_4.0-compatible-4BC51D.svg?style=flat" alt="Swift 4.0 compatible" /></a>
<a href="https://github.com/Carthage/Carthage"><img src="https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat" alt="Carthage compatible" /></a>
<a href="https://cocoapods.org/pods/tablekit"><img src="https://img.shields.io/badge/pod-2.5.0-blue.svg" alt="CocoaPods compatible" /></a>
<a href="https://cocoapods.org/pods/tablekit"><img src="https://img.shields.io/badge/pod-2.6.0-blue.svg" alt="CocoaPods compatible" /></a>
<img src="https://img.shields.io/badge/platform-iOS-blue.svg?style=flat" alt="Platform iOS" />
<a href="https://raw.githubusercontent.com/maxsokolov/tablekit/master/LICENSE"><img src="http://img.shields.io/badge/license-MIT-blue.svg?style=flat" alt="License: MIT" /></a>
</p>
Expand Down
2 changes: 1 addition & 1 deletion Sources/TableCellRegisterer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TableCellRegisterer {
// in that case we could register nib
if let _ = bundle.path(forResource: reuseIdentifier, ofType: "nib") {
tableView?.register(UINib(nibName: reuseIdentifier, bundle: bundle), forCellReuseIdentifier: reuseIdentifier)
// otherwise, register cell class
// otherwise, register cell class
} else {
tableView?.register(cellType, forCellReuseIdentifier: reuseIdentifier)
}
Expand Down
76 changes: 54 additions & 22 deletions Sources/TableDirector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
return sections.isEmpty
}

public init(tableView: UITableView, scrollDelegate: UIScrollViewDelegate? = nil, shouldUseAutomaticCellRegistration: Bool = true, cellHeightCalculator: RowHeightCalculator?) {
public init(
tableView: UITableView,
scrollDelegate: UIScrollViewDelegate? = nil,
shouldUseAutomaticCellRegistration: Bool = true,
cellHeightCalculator: RowHeightCalculator?)
{
super.init()

if shouldUseAutomaticCellRegistration {
Expand All @@ -64,11 +69,22 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
NotificationCenter.default.addObserver(self, selector: #selector(didReceiveAction), name: NSNotification.Name(rawValue: TableKitNotifications.CellAction), object: nil)
}

public convenience init(tableView: UITableView, scrollDelegate: UIScrollViewDelegate? = nil, shouldUseAutomaticCellRegistration: Bool = true, shouldUsePrototypeCellHeightCalculation: Bool = false) {

let heightCalculator: TablePrototypeCellHeightCalculator? = shouldUsePrototypeCellHeightCalculation ? TablePrototypeCellHeightCalculator(tableView: tableView) : nil
public convenience init(
tableView: UITableView,
scrollDelegate: UIScrollViewDelegate? = nil,
shouldUseAutomaticCellRegistration: Bool = true,
shouldUsePrototypeCellHeightCalculation: Bool = false)
{
let heightCalculator: TablePrototypeCellHeightCalculator? = shouldUsePrototypeCellHeightCalculation
? TablePrototypeCellHeightCalculator(tableView: tableView)
: nil

self.init(tableView: tableView, scrollDelegate: scrollDelegate, shouldUseAutomaticCellRegistration: shouldUseAutomaticCellRegistration, cellHeightCalculator: heightCalculator)
self.init(
tableView: tableView,
scrollDelegate: scrollDelegate,
shouldUseAutomaticCellRegistration: shouldUseAutomaticCellRegistration,
cellHeightCalculator: heightCalculator
)
}

deinit {
Expand All @@ -80,22 +96,34 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
}

// MARK: Public

@discardableResult
open func invoke(action: TableRowActionType, cell: UITableViewCell?, indexPath: IndexPath, userInfo: [AnyHashable: Any]? = nil) -> Any? {
return sections[indexPath.section].rows[indexPath.row].invoke(action: action, cell: cell, path: indexPath, userInfo: userInfo)
open func invoke(
action: TableRowActionType,
cell: UITableViewCell?, indexPath: IndexPath,
userInfo: [AnyHashable: Any]? = nil) -> Any?
{
if indexPath.section < sections.count && indexPath.row < sections[indexPath.section].rows.count {
return sections[indexPath.section].rows[indexPath.row].invoke(
action: action,
cell: cell,
path: indexPath,
userInfo: userInfo
)
}
return nil
}

open override func responds(to selector: Selector) -> Bool {
return super.responds(to: selector) || scrollDelegate?.responds(to: selector) == true
}

open override func forwardingTarget(for selector: Selector) -> Any? {
return scrollDelegate?.responds(to: selector) == true ? scrollDelegate : super.forwardingTarget(for: selector)
return scrollDelegate?.responds(to: selector) == true
? scrollDelegate
: super.forwardingTarget(for: selector)
}

// MARK: - Internal

func hasAction(_ action: TableRowActionType, atIndexPath indexPath: IndexPath) -> Bool {
return sections[indexPath.section].rows[indexPath.row].has(action: action)
}
Expand All @@ -108,7 +136,6 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
}

// MARK: - Height

open func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {

let row = sections[indexPath.section].rows[indexPath.row]
Expand All @@ -117,7 +144,10 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
cellRegisterer?.register(cellType: row.cellType, forCellReuseIdentifier: row.reuseIdentifier)
}

return row.defaultHeight ?? row.estimatedHeight ?? rowHeightCalculator?.estimatedHeight(forRow: row, at: indexPath) ?? UITableViewAutomaticDimension
return row.defaultHeight
?? row.estimatedHeight
?? rowHeightCalculator?.estimatedHeight(forRow: row, at: indexPath)
?? UITableViewAutomaticDimension
}

open func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
Expand All @@ -130,11 +160,13 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {

let rowHeight = invoke(action: .height, cell: nil, indexPath: indexPath) as? CGFloat

return rowHeight ?? row.defaultHeight ?? rowHeightCalculator?.height(forRow: row, at: indexPath) ?? UITableViewAutomaticDimension
return rowHeight
?? row.defaultHeight
?? rowHeightCalculator?.height(forRow: row, at: indexPath)
?? UITableViewAutomaticDimension
}

// MARK: UITableViewDataSource - configuration

open func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
Expand Down Expand Up @@ -163,7 +195,6 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
}

// MARK: UITableViewDataSource - section setup

open func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section].headerTitle
}
Expand All @@ -173,7 +204,6 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
}

// MARK: UITableViewDelegate - section setup

open func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return sections[section].headerView
}
Expand All @@ -191,11 +221,12 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
open func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

let section = sections[section]
return section.footerHeight ?? section.footerView?.frame.size.height ?? UITableViewAutomaticDimension
return section.footerHeight
?? section.footerView?.frame.size.height
?? UITableViewAutomaticDimension
}

// MARK: UITableViewDataSource - Index

public func sectionIndexTitles(for tableView: UITableView) -> [String]? {

var indexTitles = [String]()
Expand All @@ -216,12 +247,15 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
return nil
}

public func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
public func tableView(
_ tableView: UITableView,
sectionForSectionIndexTitle title: String,
at index: Int) -> Int
{
return sectionsIndexTitlesIndexes?[index] ?? 0
}

// MARK: UITableViewDelegate - actions

open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let cell = tableView.cellForRow(at: indexPath)
Expand Down Expand Up @@ -258,7 +292,6 @@ open class TableDirector: NSObject, UITableViewDataSource, UITableViewDelegate {
}

// MARK: - Row editing

open func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return sections[indexPath.section].rows[indexPath.row].isEditingAllowed(forIndexPath: indexPath)
}
Expand Down Expand Up @@ -341,7 +374,6 @@ extension TableDirector {
}

// MARK: - deprecated methods

@available(*, deprecated, message: "Use 'delete(sectionAt:)' method instead")
@discardableResult
open func delete(index: Int) -> Self {
Expand Down
7 changes: 6 additions & 1 deletion Sources/TableKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ public protocol RowActionable {
var editingActions: [UITableViewRowAction]? { get }
func isEditingAllowed(forIndexPath indexPath: IndexPath) -> Bool

func invoke(action: TableRowActionType, cell: UITableViewCell?, path: IndexPath, userInfo: [AnyHashable: Any]?) -> Any?
func invoke(
action: TableRowActionType,
cell: UITableViewCell?,
path: IndexPath,
userInfo: [AnyHashable: Any]?) -> Any?

func has(action: TableRowActionType) -> Bool
}

Expand Down
2 changes: 1 addition & 1 deletion TableKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Pod::Spec.new do |s|
s.name = 'TableKit'
s.module_name = 'TableKit'

s.version = '2.5.0'
s.version = '2.6.0'

s.homepage = 'https://github.com/maxsokolov/TableKit'
s.summary = 'Type-safe declarative table views with Swift.'
Expand Down

0 comments on commit f05511c

Please sign in to comment.