-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOGVenueTableViewDataSource.swift
More file actions
117 lines (93 loc) · 3.99 KB
/
Copy pathOGVenueTableViewDataSource.swift
File metadata and controls
117 lines (93 loc) · 3.99 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// OGVenueTableViewDaraSource.swift
// Bourbon-iOS
//
// Created by Alyssa Torres on 5/1/17.
// Copyright © 2017 Ourglass. All rights reserved.
//
import UIKit
/// `UITableView` data source that is used to provide the table with `OGVenue` data.
class OGVenueTableViewDataSource: NSObject {
/// Used to identify which venues to use in the table.
var type: OGVenueType
/// Text to display when there is no data in the table.
var noDataText: String
/// Accessory to display on the cell.
var accessory: UITableViewCellAccessoryType
init(_ tableView: UITableView, type: OGVenueType, noDataText: String, accessory: UITableViewCellAccessoryType = .none) {
self.type = type
self.noDataText = noDataText
self.accessory = accessory
super.init()
tableView.dataSource = self
}
}
extension OGVenueTableViewDataSource: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
switch type {
case OGVenueType.ALL:
return 1
case OGVenueType.MINE:
return StateController.sharedInstance.myVenues.count
case OGVenueType.OWNED:
return 1
case OGVenueType.MANAGED:
return 1
}
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch type {
case OGVenueType.MINE:
return StateController.sharedInstance.myVenues[section].label
default:
return nil
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var rows = 0
switch type {
case OGVenueType.ALL:
rows = StateController.sharedInstance.allVenues.count
case OGVenueType.MINE:
rows = StateController.sharedInstance.myVenues[section].venues.count
case OGVenueType.OWNED:
rows = StateController.sharedInstance.ownedVenues.count
case OGVenueType.MANAGED:
rows = StateController.sharedInstance.managedVenues.count
}
if rows == 0 && type != OGVenueType.MINE { // display a message indicating there is no data
// TODO this needs to be replaced with something in the storyboard
let noDataLabel = UILabel(frame: CGRect(x: 40, y: 0, width: tableView.bounds.size.width-80, height: tableView.bounds.size.height))
noDataLabel.text = self.noDataText
noDataLabel.textColor = UIColor.white
noDataLabel.textAlignment = .center
noDataLabel.font = UIFont(name: Style.regularFont, size: 14.0)
noDataLabel.numberOfLines = 5
tableView.backgroundView = noDataLabel
tableView.separatorStyle = .none
} else { // there is data, so make sure the "no data" view is gone
tableView.separatorStyle = .singleLine
tableView.backgroundView = nil
}
return rows
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let venue: OGVenue
switch type {
case OGVenueType.ALL:
venue = StateController.sharedInstance.allVenues[indexPath.row]
case OGVenueType.MINE:
venue = StateController.sharedInstance.myVenues[indexPath.section].venues[indexPath.row]
case OGVenueType.OWNED:
venue = StateController.sharedInstance.ownedVenues[indexPath.row]
case OGVenueType.MANAGED:
venue = StateController.sharedInstance.managedVenues[indexPath.row]
}
let cell = tableView.dequeueReusableCell(withIdentifier: OGVenueTableCell.identifier, for: indexPath) as! OGVenueTableCell
cell.name.text = venue.name
cell.address.text = venue.address
cell.selectionStyle = .none
cell.accessoryType = accessory
return cell
}
}