Guest User

Untitled

a guest
Feb 8th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // TableCellDelegate
  4. //
  5. // Created by Chris Cantley on 6/1/15.
  6. // Copyright (c) 2015 Chris Cantley. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController, CellInfoDelegate {
  12.  
  13. var cellViewController = CellViewController()
  14.  
  15. //The place to put the number into.
  16. @IBOutlet weak var sumLabel: UILabel!
  17.  
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20. cellViewController.delegate = self
  21. }
  22.  
  23. //2)...to here.
  24.  
  25. func processThatNumber(theNumber: Int) {
  26. println("out : (theNumber)")
  27. }
  28. }
  29.  
  30.  
  31. // Table View delegates
  32. extension ViewController: UITableViewDataSource, UITableViewDelegate
  33. {
  34.  
  35. //One row
  36. func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  37. return 1
  38. }
  39.  
  40. // Load custom cell
  41. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  42. let cell = tableView.dequeueReusableCellWithIdentifier("thisCustomCell", forIndexPath: indexPath) as! CellViewController
  43. return cell
  44. }
  45.  
  46. }
  47.  
  48.  
  49. //-------------------- Protocol for Delegate -----------------------
  50.  
  51. protocol CellInfoDelegate {
  52. func processThatNumber(theNumber: Int)
  53. }
  54.  
  55.  
  56.  
  57. //-------------------- Cell to Pass info to Parent -----------------------
  58.  
  59. class CellViewController: UITableViewCell{
  60.  
  61. var sumNumber: Int = 0
  62. var delegate: CellInfoDelegate?
  63.  
  64.  
  65. @IBAction func addButton(sender: AnyObject) {
  66.  
  67. // increment that number
  68. self.sumNumber += 5
  69.  
  70. //1) I want to get it from here...... but delegate ends up nil
  71. if let delegate = self.delegate {
  72. delegate.processThatNumber(self.sumNumber)
  73. }
  74.  
  75.  
  76. //Shows that the number is incrementing
  77. println(sumNumber)
  78.  
  79. }
  80. }
  81.  
  82. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  83. let cell = tableView.dequeueReusableCellWithIdentifier("thisCustomCell", forIndexPath: indexPath) as! CellViewController
  84. cell.delegate = self // <-- Set the delegate.
  85. return cell
  86. }
  87.  
  88. func processThatNumber(theNumber: Int) -> Int {
  89. println("out : (theNumber)")
  90. return theNumber
  91. }
  92.  
  93. @IBAction func addButton(sender: AnyObject) {
  94. self.sumNumber += 5
  95. sumLabel.text = "(processThatNumber(self.sumNumber))"
  96. println(sumNumber)
  97. }
  98.  
  99. //
  100. // ViewController.swift
  101. // TableCellDelegate
  102. //
  103. // Created by Chris Cantley on 6/1/15.
  104. // Copyright (c) 2015 Chris Cantley. All rights reserved.
  105. //
  106.  
  107. import UIKit
  108. import Foundation
  109.  
  110. class ViewController: UIViewController, CellInfoDelegate {
  111.  
  112. //The place to put the number into.
  113. @IBOutlet weak var sumLabel: UILabel!
  114.  
  115. override func viewDidLoad() {
  116. super.viewDidLoad()
  117. }
  118.  
  119. //2)...to here.
  120.  
  121. func processThatNumber(theNumber: Int) {
  122. println("out : (theNumber)")
  123. self.sumLabel.text = toString(theNumber) as String
  124. }
  125. }
  126.  
  127.  
  128. // Table View delegates
  129. extension ViewController: UITableViewDataSource, UITableViewDelegate
  130. {
  131.  
  132. //One row
  133. func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  134. return 1
  135. }
  136.  
  137. // Load custom cell
  138. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  139. let cell = tableView.dequeueReusableCellWithIdentifier("thisCustomCell", forIndexPath: indexPath) as! CellViewController
  140.  
  141. //SOLUTION : put the Delgate HERE in the place where the cell is instantiated so that there is a connection back
  142. // to this class from the Cell class
  143. cell.delegate = self
  144.  
  145. return cell
  146. }
  147.  
  148. }
  149.  
  150.  
  151. //-------------------- Protocol for Delegate -----------------------
  152.  
  153. protocol CellInfoDelegate {
  154. func processThatNumber(theNumber: Int)
  155. }
  156.  
  157.  
  158.  
  159. //-------------------- Cell to Pass info to Parent -----------------------
  160.  
  161. class CellViewController: UITableViewCell{
  162.  
  163. var sumNumber: Int = 0
  164. var delegate: CellInfoDelegate?
  165.  
  166.  
  167. @IBAction func addButton(sender: AnyObject) {
  168.  
  169. // increment that number
  170. self.sumNumber += 5
  171.  
  172. //1) I want to get it from here...... but delegate ends up nil
  173. if let delegate = self.delegate {
  174. delegate.processThatNumber(self.sumNumber)
  175. }
  176.  
  177.  
  178. //Shows that the number is incrementing
  179. println(sumNumber)
  180.  
  181. }
  182. }
Add Comment
Please, Sign In to add comment