forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadioSelectionTableViewController.swift
More file actions
70 lines (50 loc) · 2.19 KB
/
RadioSelectionTableViewController.swift
File metadata and controls
70 lines (50 loc) · 2.19 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
//
// RadioSelectionTableViewController.swift
// Loop
//
// Created by Nate Racklyeft on 8/26/16.
// Copyright © 2016 Nathan Racklyeft. All rights reserved.
//
import UIKit
public protocol RadioSelectionTableViewControllerDelegate: AnyObject {
func radioSelectionTableViewControllerDidChangeSelectedIndex(_ controller: RadioSelectionTableViewController)
}
open class RadioSelectionTableViewController: UITableViewController {
open var options = [String]()
open var selectedIndex: Int? {
didSet {
if let oldValue = oldValue, oldValue != selectedIndex {
tableView.cellForRow(at: IndexPath(row: oldValue, section: 0))?.accessoryType = .none
}
if let selectedIndex = selectedIndex, oldValue != selectedIndex {
tableView.cellForRow(at: IndexPath(row: selectedIndex, section: 0))?.accessoryType = .checkmark
delegate?.radioSelectionTableViewControllerDidChangeSelectedIndex(self)
}
}
}
open var contextHelp: String?
weak open var delegate: RadioSelectionTableViewControllerDelegate?
convenience public init() {
self.init(style: .grouped)
}
// MARK: - Table view data source
override open func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return options.count
}
override open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style: .default, reuseIdentifier: "Cell")
cell.textLabel?.text = options[indexPath.row]
cell.accessoryType = selectedIndex == indexPath.row ? .checkmark : .none
return cell
}
override open func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return contextHelp
}
override open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedIndex = indexPath.row
tableView.deselectRow(at: indexPath, animated: true)
}
}