Guest User

Untitled

a guest
Oct 24th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. //
  2. // ExampleCode.swift
  3. //
  4.  
  5. import UIKit
  6.  
  7. // MARK: - Protocol
  8.  
  9. protocol SearchQueryProviderProtocol : class { // 'class' means only class types can implement it
  10. func searchQueryData() -> String
  11. }
  12.  
  13.  
  14. // MARK: - Table view controller
  15.  
  16. class MyTableViewController : UITableViewController {
  17.  
  18. override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  19. // Try to get a cell
  20. let cell : MyTableViewCell
  21. if let theCell = tableView.dequeueReusableCellWithIdentifier(MyTableViewCell.reuseIdentifier) as? MyTableViewCell {
  22. cell = theCell
  23. } else {
  24. cell = MyTableViewCell()
  25. }
  26. cell.bind(self, title: "This is cell # \(indexPath.row) in the table view")
  27. return cell
  28. }
  29.  
  30. // Other table view delegate/data source methods
  31.  
  32. override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  33. return 1
  34. }
  35.  
  36. override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  37. return 10
  38. }
  39. }
  40.  
  41. extension MyTableViewController : SearchQueryProviderProtocol {
  42.  
  43. func searchQueryData() -> String {
  44. // TODO: actual implementation...
  45. return "hello world"
  46. }
  47. }
  48.  
  49.  
  50. // MARK - Cell
  51.  
  52. class MyTableViewCell : UITableViewCell {
  53.  
  54. weak var delegate : SearchQueryProviderProtocol?
  55.  
  56. static let reuseIdentifier = "MyTableViewCell"
  57.  
  58. func bind(delegate: SearchQueryProviderProtocol, title: String) {
  59. self.delegate = delegate
  60. textLabel?.text = title
  61. }
  62.  
  63. func doSomethingWhenTapped() {
  64. // TODO: actually set up the cell so this method is called when tapped
  65. if let parentSearchData = delegate?.searchQueryData() {
  66. // TODO: do something with your data...
  67. } else {
  68. println("Something is wrong, this cell's delegate wasn't set...")
  69. }
  70. }
  71. }
Add Comment
Please, Sign In to add comment