2015-06-01 7 views
5

Ho scoperto come passare i dati tra le viste con i delegati in altre situazioni, ma questo mi ostacola.Come utilizzare i delegati per comunicare i dati da una cella personalizzata a un'etichetta nella visualizzazione genitore

In questo esempio sto cercando di inviare i dati risultanti dalla pressione di un pulsante, fino all'etichetta utilizzando un modello delegato ma senza esito positivo. La mia ipotesi è che mi manca qualcosa di fondamentale qui e non ho trovato esempi che trattino i delegati in questo modo.

// 
// 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

Il ViewController e CellViewController sono collegati alle rispettive classi

Grazie in anticipo.

risposta

12

È necessario impostare il delegato qui:

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 
    } 
+0

GRAZIE! se capisco correttamente il ragionamento, è perché è qui che la cella viene istanziata e questo crea una connessione al genitore per ogni cella? –

+0

Ciò è corretto. –

+0

Questo collega il mio divario nella comprensione dei delegati. Grazie ancora. –

0

Avete bisogno di usare i delegati?

Che cosa succede se si dispone di questa uscita funzione di un numero:

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

quindi impostare il testo sull'etichetta utilizzando il pulsante:

@IBAction func addButton(sender: AnyObject) { 
    self.sumNumber += 5 
    sumLabel.text = "\(processThatNumber(self.sumNumber))" 
    println(sumNumber) 
} 

Sarebbe il lavoro per voi?

+0

Il nocciolo del problema è che la classe ViewController è diversa dalla classe CellViewController. Il passaggio dei dati a CellViewController non è un problema, ma ottenere un backup da esso sembra richiedere un delegato poiché non riesco ad agganciare il pulsante da nessuna parte, a parte la classe a cui è collegato il genitore. E poiché questa è una cella personalizzata, una classe separata è il modo più semplice per gestire le azioni e le funzioni specifiche all'interno. Tuttavia, se ho torto o c'è un modo più semplice, sono aperto alle soluzioni. –

6

Grazie a i_am_jorf per la soluzione, ecco il codice che funziona.

// 
// 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) 

    } 
} 
+1

Questa è onestamente la cosa più grande che abbia mai trovato su SO ... –