Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- }
- }
- 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)
- }
- }
- 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)
- }
- }
- }
- 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
- }
- }
Add Comment
Please, Sign In to add comment