如何从Swift中的TableCell类访问UIViewController类中的值?

时间:2018-07-19 09:59:42

标签: ios swift uitableview firebase firebase-realtime-database

因此,我有一个UIViewController(PledgeViewController)和一个TableView。当用户单击UIButton(plusBtn)的{​​{1}}中的UITableViewCell(PledgeTableViewCell)时,我想对Firebase数据库执行写操作。但是要获得确切的路径,我需要来自TableView类的String(getID),该类是从先前的ViewController中通过segue接收的。使用我使用的MVC格式,如何访问PledgeViewController中的值以从PledgeViewController写入数据库?

enter image description here 我的PledgeViewController.swift:

PledgeTableViewCell

我的PledgeTableViewCell.swift:

import UIKit
import Foundation
import FirebaseDatabase
import Firebase

class PledgeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{


    var getID: String!

    @IBOutlet weak var pledgeAmtLabel: UILabel!
    @IBOutlet weak var RewardChooseTable: UITableView!
    @IBAction func pledgeBtn(_ sender: Any) {
        //get the text from the label and run all the checks to see if the tickets are available

    }

    let RewardRef = Database.database().reference().child("Rewards")

    var rewards = [Rewards]()

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return rewards.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "TakePledgeCell", for: indexPath) as! PledgeTableViewCell
        let reward = rewards[indexPath.row]
        cell.reward = reward
        return cell

    }



    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        RewardRef.child(getID).observe(.value, with: { (snapshot) in
            self.rewards.removeAll()

            for child in snapshot.children {
                let childSnapshot = child as! DataSnapshot
                let reward = Rewards(snapshot: childSnapshot)
                self.rewards.insert(reward, at: 0)
            }

            self.RewardChooseTable.reloadData()
        })

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        print("The id received from the SingleViewControl is:" + getID)
}

}

Rewards.swift:

import UIKit
import Firebase
import FirebaseDatabase

class PledgeTableViewCell: UITableViewCell {


    @IBOutlet weak var rewardAmtLabel: UILabel!
    @IBOutlet weak var ticketClasslabel: UILabel!
    @IBOutlet weak var ticketDescLabel: UILabel!

    @IBOutlet weak var ticketCountLabel: UILabel!
    @IBOutlet weak var plusBtn: UIButton!
    @IBOutlet weak var minusBtn: UIButton!
    var ref: DatabaseReference!
    var artcallid: Int!

    @IBAction func minusBtn(_ sender: Any) {
    }


    var reward: Rewards! {
        didSet {

            rewardAmtLabel.text = "Rs. " + String(reward.rewardAmt)
            ticketClasslabel.text = reward.reward_class_name
            ticketDescLabel.text = reward.reward_desc
            print(reward.reward_class_name + " is one of the rewards")

        }

    }

    @IBAction func plusBtn(_ sender: AnyObject) {


    }


}

2 个答案:

答案 0 :(得分:1)

您可以尝试添加新的变量

class PledgeTableViewCell: UITableViewCell {

    var currentID = ""
}

并在cellForRowAt

中进行设置
cell.currentID = getID

答案 1 :(得分:1)

您可以尝试关闭

class PledgeTableViewCell: UITableViewCell {
  //Define a closure

    var closure:(() -> Void)? = nil

@IBAction func plusBtn(_ sender: AnyObject) {

 // Do you stuff

   closure?()
    }
}

    class PledgeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "TakePledgeCell", for: indexPath) as! PledgeTableViewCell

            let reward = rewards[indexPath.row]
            cell.reward = reward
            cell.closure = {
        // You will get the callback in this block
       // You can define the parameterized closure to return the value

    }
            return cell
 }