OptionsViewController.swift 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // OptionsViewController.swift
  3. // Demo
  4. //
  5. // Created by Iftekhar on 26/08/15.
  6. // Copyright (c) 2015 Iftekhar. All rights reserved.
  7. //
  8. protocol OptionsViewControllerDelegate: AnyObject {
  9. func optionsViewController(_ controller: OptionsViewController, index: NSInteger)
  10. }
  11. class OptionsViewController: UITableViewController {
  12. weak var delegate: OptionsViewControllerDelegate?
  13. var options = [String]()
  14. var selectedIndex: Int = 0
  15. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  16. return self.options.count
  17. }
  18. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  19. guard let cell = tableView.dequeueReusableCell(withIdentifier: "OptionTableViewCell", for: indexPath) as? OptionTableViewCell else {
  20. fatalError("Can't dequeue cell")
  21. }
  22. cell.labelOption.text = options[indexPath.row]
  23. if indexPath.row == self.selectedIndex {
  24. cell.accessoryType = .checkmark
  25. } else {
  26. cell.accessoryType = .none
  27. }
  28. return cell
  29. }
  30. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  31. tableView.deselectRow(at: indexPath, animated: true)
  32. selectedIndex = indexPath.row
  33. delegate?.optionsViewController(self, index: indexPath.row)
  34. tableView.reloadRows(at: tableView.indexPathsForVisibleRows!, with: .automatic)
  35. }
  36. }