因此,我有一个带有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)
}
}
答案 0 :(得分: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:
的{{1}}方法中
PledgeViewController