@@ -0,0 +1,153 @@
2+// ChannelController.swift
5+// Created by cstore on 19/02/2019.
6+// Copyright © 2019 drHSE. All rights reserved.
11+class ChannelController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UISearchBarDelegate
13+ @IBOutlet weak var tableView: UITableView!
15+ var dataSource: [String] = []
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"]
22+ @IBOutlet weak var viewww: UIView!
24+ @IBOutlet weak var textField: UITextField!
26+ override func viewDidLoad() {
28+ tableView.delegate = self
29+ tableView.dataSource = self
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"
40+ func numberOfSections(in tableView: UITableView) -> Int {
44+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
45+ return dataSource.count
48+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
49+ let cell = tableView.dequeueReusableCell(withIdentifier: itemCellId, for: indexPath)
51+ cell.textLabel?.text = dataSource[indexPath.row]
55+ func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
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"]
67+ func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
69+ let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 70))
72+ searchBar.selectedScopeButtonIndex = 1
74+ searchBar.selectedScopeButtonIndex = 0
77+ view.addSubview(searchBar)
82+ func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
83+ navigationItem.title = textField.text
87+ func textFieldShouldReturn(_ textField: UITextField) -> Bool
89+ textField.resignFirstResponder()
93+ func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
94+ if searchText.isEmpty {
95+ dataSource = searchBar.selectedScopeButtonIndex == 0 ? fullPeople : fullTags
96+ tableView.reloadData()
98+ filterTableView(ind: searchBar.selectedScopeButtonIndex, text: searchText)
102+ func filterTableView(ind: Int, text: String){
105+ dataSource = fullPeople.filter({ (s) -> Bool in
106+ s.lowercased().contains(text.lowercased())
108+ tableView.reloadData()
110+ dataSource = fullTags.filter({ (s) -> Bool in
111+ s.lowercased().contains(text.lowercased())
113+ tableView.reloadData()
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))
127+ filterTableView(ind: selectedScope, text: searchBar.text!)
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()
137+ func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
138+ dataSource = searchBar.selectedScopeButtonIndex == 0 ? people : tags
139+ flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
140+ tableView.reloadData()
143+ func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
144+ dataSource = searchBar.selectedScopeButtonIndex == 0 ? fullPeople : fullTags
145+ flag = searchBar.selectedScopeButtonIndex == 0 ? false : true
146+ tableView.reloadData()
149+ func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
151+ searchBar.resignFirstResponder()