|
| 1 | +// |
| 2 | +// ViewController.swift |
| 3 | +// Examples |
| 4 | +// |
| 5 | +// Created by Ryan Nystrom on 1/28/18. |
| 6 | +// Copyright © 2018 Ryan Nystrom. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | +import MessageViewController |
| 11 | + |
| 12 | +class ViewController: MessageViewController, UITableViewDataSource, UITableViewDelegate, MessageAutocompleteControllerDelegate { |
| 13 | + |
| 14 | + var data = "Lorem ipsum dolor sit amet|consectetur adipiscing elit|sed do eiusmod|tempor incididunt|ut labore et dolore|magna aliqua| Ut enim ad minim|veniam, quis nostrud|exercitation ullamco|laboris nisi ut aliquip|ex ea commodo consequat|Duis aute|irure dolor in reprehenderit|in voluptate|velit esse cillum|dolore eu|fugiat nulla pariatur|Excepteur sint occaecat|cupidatat non proident|sunt in culpa|qui officia|deserunt|mollit anim id est laborum" |
| 15 | + .components(separatedBy: "|") |
| 16 | + let users = ["rnystrom", "BasThomas", "jessesquires", "Sherlouk"] |
| 17 | + var autocompleteUsers = [String]() |
| 18 | + let tableView = UITableView() |
| 19 | + |
| 20 | + override func viewDidLoad() { |
| 21 | + super.viewDidLoad() |
| 22 | + tableView.dataSource = self |
| 23 | + tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") |
| 24 | + view.addSubview(tableView) |
| 25 | + |
| 26 | + borderColor = .lightGray |
| 27 | + |
| 28 | + messageView.inset = UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16) |
| 29 | + messageView.textView.placeholderText = "New message..." |
| 30 | + messageView.textView.placeholderTextColor = .lightGray |
| 31 | + messageView.font = UIFont.systemFont(ofSize: 17) |
| 32 | + |
| 33 | + messageView.set(buttonTitle: "Send", for: .normal) |
| 34 | + messageView.addButton(target: self, action: #selector(onButton)) |
| 35 | + messageView.buttonTint = .blue |
| 36 | + |
| 37 | + messageAutocompleteController.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") |
| 38 | + messageAutocompleteController.tableView.dataSource = self |
| 39 | + messageAutocompleteController.tableView.delegate = self |
| 40 | + messageAutocompleteController.register(prefix: "@") |
| 41 | + messageAutocompleteController.delegate = self |
| 42 | + |
| 43 | + setup(scrollView: tableView) |
| 44 | + } |
| 45 | + |
| 46 | + @objc func onButton() { |
| 47 | + data.append(messageView.text) |
| 48 | + messageView.text = "" |
| 49 | + tableView.reloadData() |
| 50 | + tableView.scrollToRow( |
| 51 | + at: IndexPath(row: data.count - 1, section: 0), |
| 52 | + at: .bottom, |
| 53 | + animated: true |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + // MARK: UITableViewDataSource |
| 58 | + |
| 59 | + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
| 60 | + return tableView === self.tableView |
| 61 | + ? data.count |
| 62 | + : autocompleteUsers.count |
| 63 | + } |
| 64 | + |
| 65 | + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { |
| 66 | + let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) |
| 67 | + if tableView === self.tableView { |
| 68 | + cell.textLabel?.text = data[indexPath.row] |
| 69 | + } else { |
| 70 | + cell.textLabel?.text = autocompleteUsers[indexPath.row] |
| 71 | + } |
| 72 | + return cell |
| 73 | + } |
| 74 | + |
| 75 | + // MARK: UITableViewDelegate |
| 76 | + |
| 77 | + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { |
| 78 | + tableView.deselectRow(at: indexPath, animated: true) |
| 79 | + if tableView === messageAutocompleteController.tableView { |
| 80 | + messageAutocompleteController.accept(autocomplete: autocompleteUsers[indexPath.row]) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // MARK: MessageAutocompleteControllerDelegate |
| 85 | + |
| 86 | + func didFind(controller: MessageAutocompleteController, prefix: String, word: String) { |
| 87 | + autocompleteUsers = users.filter { word.isEmpty || $0.lowercased().contains(word.lowercased()) } |
| 88 | + controller.show(true) |
| 89 | + } |
| 90 | + |
| 91 | +} |
| 92 | + |
0 commit comments