ios - 如何使用委托(delegate)将数据从自定义单元格传递到父 View 中的标签

标签 ios uitableview swift delegates protocols

我已经想出了在其他情况下如何在与委托(delegate)的 View 之间传递数据,但是这个让我很困惑。

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

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

ViewController 和 CellViewController 连接到它们各自的类

提前致谢。

最佳答案

你应该在这里设置委托(delegate):

  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
  }

关于ios - 如何使用委托(delegate)将数据从自定义单元格传递到父 View 中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30582740/

相关文章:

iphone - 将 UITableViewCell 连接到 Action

ios - 向 UITableViewCell Swift 添加渐变背景

ios - 线程 1 断点 1.1 错误 - swift

swift - 初始化后如何更改实例变量?

iphone - 转换 HH :MM NSString to NSInteger with minutes

iphone - iOS 状态保存和容器 View

ios - 由于 NSLayoutConstraint 错误,具有 automaticDimension 的 UITableView 不会动态增加大小

ios - 发送后台短信 ios 12

objective-c - 将 Objective C 中带回调的 block 转换为 Swift 中的闭包

swift - 测试协议(protocol)与关联类型的一致性