【问题标题】:How to use delegates to communicate data from a custom cell to a label in the parent view如何使用委托将数据从自定义单元格传递到父视图中的标签
【发布时间】:2015-06-01 20:31:15
【问题描述】:

我已经想出了如何在其他情况下与代表在视图之间传递数据,但是这个让我很难过。

在此示例中,我尝试使用委托模式将按下按钮产生的数据发送到标签,但没有任何成功。我的猜测是我在这里遗漏了一些基本的东西,而且我还没有找到任何以这种方式处理代表的示例。

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

    }
}

ViewController 和 CellViewController 连接到各自的类

提前致谢。

【问题讨论】:

    标签: ios uitableview swift delegates protocols


    【解决方案1】:

    你应该在这里设置委托:

      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
      }
    

    【讨论】:

    • 谢谢!如果我正确理解了推理,是因为这是实例化单元格的地方,并且这为每个单元格创建了与父级的连接?
    • 这连接了我对代表的理解差距。再次感谢。
    • @Wade:不管怎样,因为 Swift 让它变得如此简单,你也可以选择在你的单元格上存储一个闭包属性:cell.process = { cell in println("Called from cell")}。单元格依次如下所示:class Cell : Base { var process:(()-&gt;())?; func addButton(){ self.process?() } }
    • 这是个好主意,对于更简单的结构,我下次会完全使用它。我上面的例子是一个更复杂的简化版本,但我将使用你发布的方法。谢谢!
    • 我真的花了好几天才找到这个小宝石来解决我的问题
    【解决方案2】:

    感谢 i_am_jorf 的解决方案,这是有效的代码。

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

    【讨论】:

      【解决方案3】:

      您需要使用委托吗?

      如果你有这个函数输出一个数字怎么办:

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

      然后使用按钮设置标签上的文字:

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

      这对你有用吗?

      【讨论】:

      • 问题的核心在于ViewController类与CellViewController类不同。将数据向下传递到 CellViewController 不是问题,但从中获取一些东西似乎需要一个委托,因为除了它的父级连接到的类之外,我无法在任何地方连接按钮。由于这是一个自定义单元格,因此单独的类是管理其中的操作和特定功能的最简单方法。但是,如果我错了或有更简单的方法,我愿意接受劝告。
      猜你喜欢
      • 1970-01-01
      • 2014-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      相关资源
      最近更新 更多