-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTableActions.swift
97 lines (81 loc) · 3.22 KB
/
TableActions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Copyright (c) 2011-present, NimbusKit. All rights reserved.
This source code is licensed under the BSD-style license found at http://nimbuskit.info/license
*/
import Foundation
import UIKit
extension Actions {
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forObject object: NSObject, atIndexPath indexPath: NSIndexPath) -> Bool {
if !self.isActionableObject(object) {
return false
}
cell.accessoryType = self.accessoryTypeForObject(object)
cell.selectionStyle = self.selectionStyleForObject(object)
return true
}
func tableView(tableView: UITableView, didSelectObject object: NSObject, atIndexPath indexPath: NSIndexPath) {
let actions = self.actionsForObject(object)
if !actions.hasActions() {
return
}
if let shouldDeselect = actions.performTapAction(object, indexPath: indexPath) {
if shouldDeselect {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
actions.performNavigateAction(object, indexPath: indexPath)
}
func tableView(tableView: UITableView, accessoryButtonTappedForObject object: NSObject, withIndexPath indexPath: NSIndexPath) {
let actions = self.actionsForObject(object)
if !actions.hasActions() {
return
}
actions.performDetailAction(object, indexPath: indexPath)
}
}
extension Actions : UITableViewDelegate {
public func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.accessoryType = .None
cell.selectionStyle = .None
if let object = self.actionableObjectForTableView(tableView, atIndexPath: indexPath) {
self.tableView(tableView, willDisplayCell: cell, forObject: object, atIndexPath: indexPath)
}
}
public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let object = self.actionableObjectForTableView(tableView, atIndexPath: indexPath) {
self.tableView(tableView, didSelectObject: object, atIndexPath: indexPath)
}
}
public func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
if let object = self.actionableObjectForTableView(tableView, atIndexPath: indexPath) {
self.tableView(tableView, accessoryButtonTappedForObject: object, withIndexPath: indexPath)
}
}
}
// Private
extension Actions {
private func accessoryTypeForObject(object: NSObject) -> UITableViewCellAccessoryType {
let actions = self.actionsForObject(object)
if actions.hasDetailAction() {
return .DetailDisclosureButton
} else if actions.hasNavigateAction() {
return .DisclosureIndicator
}
return .None
}
private func selectionStyleForObject(object: NSObject) -> UITableViewCellSelectionStyle {
let actions = self.actionsForObject(object)
if (actions.hasNavigateAction() || actions.hasTapAction()) {
return .Default
}
return .None
}
private func actionableObjectForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> NSObject? {
if let model = tableView.dataSource as? TableModel {
if let object = model.objectAtPath(indexPath) as? NSObject {
return object
}
}
return nil
}
}