Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use delegates to communicate data from a custom cell to a label in the parent view

I have figured out how to pass data between views with delegates in other situations but this one is stumping me.

In this example I am trying to send data resulting from pressing a button, up to the label using a delegate pattern but without any success. My guess is that I am missing something fundamental here and I haven't found any examples that deal with delegates in quite this way.

//
//  ViewController.swift
//  TableCellDelegate
//
//  Created by Chris Cantley on 6/1/15.
//  Copyright (c) 2015 Chris Cantley. All rights reserved.
//

import UIKit

class ViewController: UIViewController, CellInfoDelegate {

    var cellViewController = CellViewController()

    //The place to put the number into.
    @IBOutlet weak var sumLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        cellViewController.delegate = self
    }

    //2)...to here.

    func processThatNumber(theNumber: Int) {
        println("out : \(theNumber)")
    }
}


// Table View delegates
extension ViewController: UITableViewDataSource, UITableViewDelegate
{

    //One row
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    // Load custom cell
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("thisCustomCell", forIndexPath: indexPath) as! CellViewController
        return cell
    }

}


//-------------------- Protocol for Delegate -----------------------

protocol CellInfoDelegate {
    func processThatNumber(theNumber: Int)
}



//-------------------- Cell to Pass info to Parent -----------------------

class CellViewController: UITableViewCell{

    var sumNumber: Int = 0
    var delegate: CellInfoDelegate?


    @IBAction func addButton(sender: AnyObject) {

        // increment that number
        self.sumNumber += 5

        //1) I want to get it from here...... but delegate ends up nil
        if let delegate = self.delegate {
            delegate.processThatNumber(self.sumNumber)
        }


        //Shows that the number is incrementing
        println(sumNumber)

    }
}

enter image description here

The ViewController and CellViewController are connected to their respective classes

Thanks in advance.

like image 402
Christopher Wade Cantley Avatar asked Jun 01 '15 20:06

Christopher Wade Cantley


2 Answers

You should set the delegate here:

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCellWithIdentifier("thisCustomCell", forIndexPath: indexPath) as! CellViewController
     cell.delegate = self  // <-- Set the delegate.
     return cell
  }
like image 199
i_am_jorf Avatar answered Nov 15 '22 08:11

i_am_jorf


Thanks to i_am_jorf for the solution, here is the code that works.

//
//  ViewController.swift
//  TableCellDelegate
//
//  Created by Chris Cantley on 6/1/15.
//  Copyright (c) 2015 Chris Cantley. All rights reserved.
//

import UIKit
import Foundation

class ViewController: UIViewController, CellInfoDelegate {

    //The place to put the number into.
    @IBOutlet weak var sumLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    //2)...to here.

    func processThatNumber(theNumber: Int) {
        println("out : \(theNumber)")
        self.sumLabel.text = toString(theNumber) as String
    }
}


// Table View delegates
extension ViewController: UITableViewDataSource, UITableViewDelegate
{

    //One row
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    // Load custom cell
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("thisCustomCell", forIndexPath: indexPath) as! CellViewController

        //SOLUTION : put the Delgate HERE in the place where the cell is instantiated so that there is a connection back
        // to this class from the Cell class
        cell.delegate = self

        return cell
    }

}


//-------------------- Protocol for Delegate -----------------------

protocol CellInfoDelegate {
    func processThatNumber(theNumber: Int)
}



//-------------------- Cell to Pass info to Parent -----------------------

class CellViewController: UITableViewCell{

    var sumNumber: Int = 0
    var delegate: CellInfoDelegate?


    @IBAction func addButton(sender: AnyObject) {

        // increment that number
        self.sumNumber += 5

        //1) I want to get it from here...... but delegate ends up nil
        if let delegate = self.delegate {
            delegate.processThatNumber(self.sumNumber)
        }


        //Shows that the number is incrementing
        println(sumNumber)

    }
}
like image 42
Christopher Wade Cantley Avatar answered Nov 15 '22 09:11

Christopher Wade Cantley