@@ -8,15 +8,22 @@
88
99import UIKit
1010
11+protocol DataDelegate {
12+ func passData(for row: Int, channel: Channel)
13+}
14+
1115class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UISearchBarDelegate
1216{
1317 @IBOutlet weak var tableView: UITableView!
1418
19+ var index: Int?
20+ var channel: Channel?
21+ var myProtocol: DataDelegate?
22+
1523 var dataSource: [String] = []
1624
17− var people: [String] = ["Ann", "Ilya", "Kostya"]
1825 var fullPeople: [String] = ["Other Ann", "Other Ilya", "Other Kostya", "Pasha", "Valera"]
19− var tags: [String] = ["# foo", "# bar", "# kek"]
26+
2027 var fullTags: [String] = ["# happy", "# sad", "# meme", "# shershakov", "# pupsik"]
2128
2229 @IBOutlet weak var viewww: UIView!
@@ -27,15 +34,22 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
2734 super.viewDidLoad()
2835 tableView.delegate = self
2936 tableView.dataSource = self
30− dataSource = people
37+ dataSource = channel!.people
3138 tableView.register(UITableViewCell.self, forCellReuseIdentifier: itemCellId)
3239 tableView.reloadData()
3340 textField.delegate = self
3441 searchBar.delegate = self
3542 searchBar.selectedScopeButtonIndex = 0
36− navigationItem.title = "Title"
43+ navigationItem.title = channel?.title
44+ textField.placeholder = channel?.title
45+ textField.addTarget(self, action: #selector(changedText(_:)), for: .editingChanged)
3746 }
3847
48+ @objc func changedText(_ textField: UITextField){
49+ navigationItem.title = textField.text
50+ channel?.title = textField.text!
51+ myProtocol?.passData(for: index!, channel: channel!)
52+ }
3953
4054 func numberOfSections(in tableView: UITableView) -> Int {
4155 return 1
@@ -45,10 +59,73 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
4559 return dataSource.count
4660 }
4761
62+ func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
63+ if searchBar.isFirstResponder {
64+ if let cell = tableView.cellForRow(at: indexPath)
65+ {
66+
67+ if cell.accessoryType == .none{
68+ switch searchBar.selectedScopeButtonIndex{
69+ case 0:
70+ channel?.people.append(dataSource[indexPath.row])
71+ case 1:
72+ channel?.hashtags.append(dataSource[indexPath.row])
73+ default:
74+ break
75+ }
76+
77+ cell.accessoryType = .checkmark
78+ } else {
79+ cell.accessoryType = .none
80+
81+ switch searchBar.selectedScopeButtonIndex{
82+ case 0:
83+ channel?.people = channel!.people.filter({ (s) -> Bool in
84+ !fullPeople[indexPath.row].contains(s)
85+ })
86+ // TODO: dataPass
87+ case 1:
88+ channel?.hashtags = channel!.people.filter({ (s) -> Bool in
89+ !fullTags[indexPath.row].contains(s)
90+ })
91+ // TODO: dataPass
92+ default:
93+ break
94+ }
95+ }
96+ }
97+ } else
98+ {
99+ tableView.beginUpdates()
100+ dataSource.remove(at: indexPath.row)
101+ switch searchBar.selectedScopeButtonIndex{
102+ case 0:
103+ channel?.people.remove(at: indexPath.row)
104+ // TODO: dataPass
105+ case 1:
106+ channel?.hashtags.remove(at: indexPath.row)
107+ // TODO: dataPass
108+ default:
109+ break
110+ }
111+ tableView.deleteRows(at: [indexPath], with: .none)
112+ tableView.endUpdates()
113+ }
114+ }
115+
48116 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
49117 let cell = tableView.dequeueReusableCell(withIdentifier: itemCellId, for: indexPath)
50118
51119 cell.textLabel?.text = dataSource[indexPath.row]
120+ cell.selectionStyle = .none
121+
122+ if channel!.hashtags.contains(dataSource[indexPath.row]) || channel!.people.contains(dataSource[indexPath.row])
123+ {
124+ cell.accessoryType = .checkmark
125+ } else {
126+ cell.accessoryType = .none
127+ }
128+
52129 return cell
53130 }
54131
@@ -78,12 +155,6 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
78155 return view
79156 }
80157
81− //change
82− func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
83− navigationItem.title = textField.text
84− return true
85− }
86−
87158 func textFieldShouldReturn(_ textField: UITextField) -> Bool
88159 {
89160 textField.resignFirstResponder()
@@ -128,14 +199,14 @@ class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataS
128199 }
129200 tableView.reloadData()
130201 } else if !(searchBar.isFirstResponder){
131− dataSource = searchBar.selectedScopeButtonIndex == 0 ? people : tags
202+ dataSource = searchBar.selectedScopeButtonIndex == 0 ? channel!.people : channel!.hashtags
132203 flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
133204 tableView.reloadData()
134205 }
135206 }
136207
137208 func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
138− dataSource = searchBar.selectedScopeButtonIndex == 0 ? people : tags
209+ dataSource = searchBar.selectedScopeButtonIndex == 0 ? channel!.people : channel!.hashtags
139210 flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
140211 tableView.reloadData()
141212 }