@@ -0,0 +1,153 @@
1+//
2+// ChannelController.swift
3+// GDproject
4+//
5+// Created by cstore on 19/02/2019.
6+// Copyright © 2019 drHSE. All rights reserved.
7+//
8+
9+import UIKit
10+
11+class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UISearchBarDelegate
12+{
13+ @IBOutlet weak var tableView: UITableView!
14+
15+ var dataSource: [String] = []
16+
17+ var people: [String] = ["Ann", "Ilya", "Kostya"]
18+ var fullPeople: [String] = ["Other Ann", "Other Ilya", "Other Kostya", "Pasha", "Valera"]
19+ var tags: [String] = ["# foo", "# bar", "# kek"]
20+ var fullTags: [String] = ["# happy", "# sad", "# meme", "# shershakov", "# pupsik"]
21+
22+ @IBOutlet weak var viewww: UIView!
23+
24+ @IBOutlet weak var textField: UITextField!
25+
26+ override func viewDidLoad() {
27+ super.viewDidLoad()
28+ tableView.delegate = self
29+ tableView.dataSource = self
30+ dataSource = people
31+ tableView.register(UITableViewCell.self, forCellReuseIdentifier: itemCellId)
32+ tableView.reloadData()
33+ textField.delegate = self
34+ searchBar.delegate = self
35+ searchBar.selectedScopeButtonIndex = 0
36+ navigationItem.title = "Title"
37+ }
38+
39+
40+ func numberOfSections(in tableView: UITableView) -> Int {
41+ return 1
42+ }
43+
44+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
45+ return dataSource.count
46+ }
47+
48+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
49+ let cell = tableView.dequeueReusableCell(withIdentifier: itemCellId, for: indexPath)
50+
51+ cell.textLabel?.text = dataSource[indexPath.row]
52+ return cell
53+ }
54+
55+ func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
56+ return 100
57+ }
58+
59+ let searchBar: UISearchBar = {
60+ let searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 70))
61+ searchBar.showsScopeBar = true
62+ searchBar.showsCancelButton = true
63+ searchBar.scopeButtonTitles = ["People", "Tags"]
64+ return searchBar
65+ }()
66+
67+ func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
68+ {
69+ let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 70))
70+
71+ if flag {
72+ searchBar.selectedScopeButtonIndex = 1
73+ } else {
74+ searchBar.selectedScopeButtonIndex = 0
75+ }
76+
77+ view.addSubview(searchBar)
78+ return view
79+ }
80+
81+ //change
82+ func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
83+ navigationItem.title = textField.text
84+ return true
85+ }
86+
87+ func textFieldShouldReturn(_ textField: UITextField) -> Bool
88+ {
89+ textField.resignFirstResponder()
90+ return true
91+ }
92+
93+ func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
94+ if searchText.isEmpty {
95+ dataSource = searchBar.selectedScopeButtonIndex == 0 ? fullPeople : fullTags
96+ tableView.reloadData()
97+ } else {
98+ filterTableView(ind: searchBar.selectedScopeButtonIndex, text: searchText)
99+ }
100+ }
101+
102+ func filterTableView(ind: Int, text: String){
103+ switch ind {
104+ case 0:
105+ dataSource = fullPeople.filter({ (s) -> Bool in
106+ s.lowercased().contains(text.lowercased())
107+ })
108+ tableView.reloadData()
109+ case 1:
110+ dataSource = fullTags.filter({ (s) -> Bool in
111+ s.lowercased().contains(text.lowercased())
112+ })
113+ tableView.reloadData()
114+ default:
115+ break
116+ }
117+ }
118+
119+ var flag = false
120+
121+ func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
122+ if searchBar.isFirstResponder {
123+ dataSource = searchBar.selectedScopeButtonIndex == 0 ? fullPeople : fullTags
124+ flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
125+ if (!(searchBar.text?.isEmpty ?? true))
126+ {
127+ filterTableView(ind: selectedScope, text: searchBar.text!)
128+ }
129+ tableView.reloadData()
130+ } else if !(searchBar.isFirstResponder){
131+ dataSource = searchBar.selectedScopeButtonIndex == 0 ? people : tags
132+ flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
133+ tableView.reloadData()
134+ }
135+ }
136+
137+ func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
138+ dataSource = searchBar.selectedScopeButtonIndex == 0 ? people : tags
139+ flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
140+ tableView.reloadData()
141+ }
142+
143+ func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
144+ dataSource = searchBar.selectedScopeButtonIndex == 0 ? fullPeople : fullTags
145+ flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
146+ tableView.reloadData()
147+ }
148+
149+ func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
150+ searchBar.text = ""
151+ searchBar.resignFirstResponder()
152+ }
153+}