如何通过单击 TableCell 中的按钮将我的 TableCell 中的标签值传递给 TableViewController? [关闭]

Posted

技术标签:

【中文标题】如何通过单击 TableCell 中的按钮将我的 TableCell 中的标签值传递给 TableViewController? [关闭]【英文标题】:How to pass the value of a label in my TableCell to the TableViewController on the click of a button in the TableCell? [closed] 【发布时间】:2018-07-20 06:38:35 【问题描述】:

所以,我有一个带有 UITableView 的 ViewController(PledgeViewController) 和另一个名为 PledgeTableViewCell.swift 的文件,它是一个 UITableCell 文件。当有人单击 PledgeTableViewCell.swift 中的按钮(plsBtn)时,我设置了一个闭包,该闭包反馈到 PledgeViewController 并从那里处理我的操作。我现在想做的是:

当有人单击 PledgeTableViewCell 中的 plsBtn 时,我正在 PledgeViewController 中处理该操作。但我想在 indexPath 处读取 TableViewCell 中标签的值并将其发送到 PledgeViewController 以进行进一步计算。我可以用闭包本身来做到这一点还是有另一种方法?谢谢!

PledgeTableViewCell.swift:

import UIKit
import Firebase
import FirebaseDatabase


class PledgeTableViewCell: UITableViewCell 

    var plusBtnAction: ((Any) -> Void)?
    var minusBtnAction: ((Any) -> Void)?

    @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 currentID = ""
    var ticket_count: Int = 0
    let userID = Auth.auth().currentUser!.uid

    @IBAction func minusBtn(_ sender: Any) 

        if var tickCount = Int(ticketCountLabel.text!) 

            if(tickCount > 0)
            
                tickCount -= 1
                ticketCountLabel.text = String(tickCount)
            
        

        self.minusBtnAction?(sender)
    

    @IBAction func plusBtn(_ sender: AnyObject) 


        if var tickCount = Int(ticketCountLabel.text!) 

//I WANT TO SEND THIS 'TICKCOUNT' TO THE PLEDGEVIEWCONTROLLER
            tickCount += 1
            ticketCountLabel.text = String(tickCount)
        


        self.plusBtnAction?(sender)

    



PledgeViewController.swift:

import UIKit
import Foundation
import FirebaseDatabase
import Firebase

class PledgeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate


    var getID: String!
    var rewards = [Rewards]()
    var ticket_count: Int = 0
    var ref: DatabaseReference!
    let userID = Auth.auth().currentUser!.uid
    var rewardID: 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

    

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
        /*  myIndex = indexPath.row

         finalId = idTable[myIndex]
         let story = stories[indexPath.row]
         ArtcallID = story.id

         performSegue(withIdentifier: "singleArtcall", sender: self)
         */
        let reward = rewards[indexPath.row]
        let id = reward.rewardID
        reward.countUp()

        print("The reward that was touched is: " + id )
        print("One of the buttons were touched")
    

    let RewardRef = Database.database().reference().child("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
        cell.currentID = getID
        rewardID = reward.rewardID

        cell.plusBtnAction =  sender in

            let reward = self.rewards[indexPath.row]
            cell.reward = reward
            let local_id = reward.rewardID

            self.ref=Database.database().reference().child("Fund_Project_Request").child(self.getID).child(self.userID).child(local_id).child("Ticket_count")


            self.ref.observeSingleEvent(of: .value, with:  (snapshot) in
                // Get user value

                if snapshot.value is NSNull
                    self.ticket_count = 0
                    self.ticket_count += 1
                    self.ref.setValue(self.ticket_count)
                

                else
                    self.ticket_count = snapshot.value as! Int
                    self.ticket_count += 1
                    self.ref.setValue(self.ticket_count)
                


            )  (error) in
                print(error.localizedDescription)
            

           Database.database().reference().child("Rewards").child(self.getID).child(local_id).child("reward_ticket_amount").observeSingleEvent(of: .value, with:  (snapshot) in
                // Get user value
                let reward_amt = snapshot.value as! Int

                self.pledgeAmtLabel.text = String(reward_amt)

            )  (error) in
                print(error.localizedDescription)
            

            // Do whatever you want from your button here.
        



        cell.minusBtnAction =  sender in

            let reward = self.rewards[indexPath.row]
            cell.reward = reward
            let local_id = reward.rewardID

        self.ref=Database.database().reference().child("Fund_Project_Request").child(self.getID).child(self.userID).child(local_id).child("Ticket_count")
            self.ref.observeSingleEvent(of: .value, with:  (snapshot) in
                // Get user value

                if snapshot.value is NSNull

                

                else

                    self.ticket_count = snapshot.value as! Int
                    if(self.ticket_count != 0)
                    
                        self.ticket_count -= 1
                        self.ref.setValue(self.ticket_count)
                    
                


            )  (error) in
                print(error.localizedDescription)
            



        



        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)



【问题讨论】:

【参考方案1】:

您可以将标签值与发送者一起传递给闭包本身。

class PledgeTableViewCell: UITableViewCell 

    var plusBtnAction: ((String) -> Void)?
    var minusBtnAction: ((String) -> Void)?



     @IBAction func minusBtn(_ sender: Any) 

          if var tickCount = Int(ticketCountLabel.text!) 

            if(tickCount > 0)
            
                tickCount -= 1
                ticketCountLabel.text = String(tickCount)

            
            self.minusBtnAction?(tickCount)
        
    

    @IBAction func plusBtn(_ sender: AnyObject) 

        if var tickCount = Int(ticketCountLabel.text!) 

//I WANT TO SEND THIS 'TICKCOUNT' TO THE PLEDGEVIEWCONTROLLER
            tickCount += 1
            ticketCountLabel.text = String(tickCount)
            self.plusBtnAction?(ticketCount)
        

       
    

cellForRow:PledgeViewController 中的方法

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

        let cell = tableView.dequeueReusableCell(withIdentifier: "TakePledgeCell", for: indexPath) as! PledgeTableViewCell
        .....
        cell.plusBtnAction =  labelText in
           //handle labelText here
        

【讨论】:

您能进一步解释一下吗? 我将如何在 PledgeViewController 中接收它? 添加的代码请检查。您可以在 celForRow 的闭包内处理 senderAny 的使用在这里毫无意义。 plusBtnActionminusBtnAction closures 应该传递单元格和文本,而不是发送者。 为什么它应该传递单元格引用?它已经在 cellForRow 中给出了单元格。

以上是关于如何通过单击 TableCell 中的按钮将我的 TableCell 中的标签值传递给 TableViewController? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

如何通过仅单击该行中的一个值来显示该行中的所有值

检测customcell上的按钮单击事件

如何更改onclick功能按钮中的值

使特定样式仅适用于表体中的 TableCell

如何通过单击 Python 3 中的按钮打开文件

如何通过单击 Android 中的按钮使 VideoView 全屏显示?