-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTableCellFactory.swift
96 lines (75 loc) · 2.85 KB
/
TableCellFactory.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
/*
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
@objc public protocol TableCellObject {
func tableCellClass() -> UITableViewCell.Type
optional func cellStyle() -> UITableViewCellStyle
optional func shouldAppendClassNameToReuseIdentifier() -> Bool
}
@objc public protocol TableCell {
func updateCellWithObject(object: TableCellObject)
}
/**
The TableCellFactory class is the binding logic between Objects and Cells and should be used as the
delegate for a TableModel.
A contrived example of creating an empty model with the singleton TableCellFactory instance.
let model = TableModel(delegate: TableCellFactory.tableModelDelegate())
*/
public class TableCellFactory : NSObject {
/**
Returns a singleton TableModelDelegate instance for use as a TableModel delegate.
*/
public class func tableModelDelegate() -> TableModelDelegate {
return self.sharedInstance
}
}
extension TableCellFactory : TableModelDelegate {
public func tableModel(tableModel: TableModel, cellForTableView tableView: UITableView, indexPath: NSIndexPath, object: AnyObject) -> UITableViewCell? {
return self.cell(object.tableCellClass(), tableView: tableView, indexPath: indexPath, object: object as? TableCellObject)
}
}
// Private
extension TableCellFactory {
/**
Returns a cell for a given object.
*/
private func cell(tableCellClass: UITableViewCell.Type, tableView: UITableView, indexPath: NSIndexPath, object: TableCellObject?) -> UITableViewCell? {
if object == nil {
return nil
}
var style = UITableViewCellStyle.Default;
var identifier = NSStringFromClass(tableCellClass)
// Append object class to reuse identifier
if (object!.shouldAppendClassNameToReuseIdentifier? != nil) && object!.shouldAppendClassNameToReuseIdentifier!() {
let typedObject: AnyObject = object as AnyObject
identifier = identifier.stringByAppendingString(NSStringFromClass(typedObject.dynamicType))
}
// Append cell style to reuse identifier
if let objectCellStyle = object!.cellStyle?() {
style = objectCellStyle
identifier = identifier.stringByAppendingString(String(style.rawValue))
}
// Recycle or create the cell
var cell = tableView.dequeueReusableCellWithIdentifier(identifier) as UITableViewCell?
if cell == nil {
cell = tableCellClass(style: style, reuseIdentifier: identifier)
}
// Provide the object to the cell
if let tableCell = cell as TableCell? {
tableCell.updateCellWithObject(object!)
}
return cell!
}
}
// Singleton Pattern
extension TableCellFactory {
private class var sharedInstance : TableCellFactory {
struct Singleton {
static let instance = TableCellFactory()
}
return Singleton.instance
}
}