0

Is there any way I can access the buttons and labels that were defined in Custom Cell from my viewController.swift file? I want to access the text of all the other buttons and set the text of the "plrAvg" all within the "handleButtonPressed" function (see the function in the code below).


import UIKit

class CustomCell: UITableViewCell {



@IBOutlet weak var plrNumber: UILabel!
@IBOutlet weak var plrAvg: UILabel!


@IBOutlet weak var btn1: UIButton!
@IBOutlet weak var btn2: UIButton!
@IBOutlet weak var btn3: UIButton!
@IBOutlet weak var btn4: UIButton!




override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
  }

}

import UIKit

 class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var lblHeader: UILabel!
@IBOutlet weak var tableView: UITableView!





/******* Establish Variables *******/

var players: NSMutableArray! = NSMutableArray()



override func viewDidLoad()
    {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 6
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell


    // Poputate the players //
    cell.plrNumber.text = (players[indexPath.row] as! String)

   // add a line for each one of the buttons
    cell.btn1.addTarget(self, action: "handleButtonPressed:", forControlEvents: .TouchUpInside)
    cell.btn2.addTarget(self, action: "handleButtonPressed:", forControlEvents: .TouchUpInside)
    cell.btn3.addTarget(self, action: "handleButtonPressed:", forControlEvents: .TouchUpInside)
    cell.btn4.addTarget(self, action: "handleButtonPressed:", forControlEvents: .TouchUpInside)


    return cell
    }

// ***********************************

@IBAction func handleButtonPressed(sender:UIButton!)
{

 ************************************************************
 IS THERE ANY CODE THAT WOULD ALLOW ME TO ACCESS ALL THE OTHER    BUTTONS (btn1, btn2, btn3, btn4) AS WELL AS THE LABEL (plrAvg)
 ************************************************************

 switch sender.tag {
    case 0:
        print(sender.tag)
    case 1:
        print(sender.tag)
    case 2:
        print(sender.tag)
    case 3:
        print(sender.tag)

    default:
        print("Tag was not found")
    }
  }
}
CNE
  • 63
  • 1
  • 7
  • 1
    you can check this link http://stackoverflow.com/questions/29913066/how-to-access-the-content-of-a-custom-cell-in-swift-using-button-tag – Subin K Kuriakose Feb 19 '16 at 04:28
  • Thanks. However, I am not sure it addresses what I am actually trying to do. What I would like to do is reference the buttons and the label in the "handleButtonPressed" function. I am new to SWIFT so it is kind of confusing since the scope of accessing items are not what I am use to. – CNE Feb 19 '16 at 22:51

1 Answers1

0
@IBAction func handleButtonPressed(sender:UIButton!)
{

************************************************************
IS THERE ANY CODE THAT WOULD ALLOW ME TO ACCESS ALL THE OTHER   BUTTONS (btn1, btn2, btn3, btn4) AS WELL AS THE LABEL (plrAvg)
************************************************************

switch sender.tag {

    let button = sender as! UIButton
    let view = button.superview!
    let cell = view.superview as! custom cell
    // cell.btn1  - will get here 
    //cell.btn2 - will get here
    // do based on what on button action
case 0:
    print(sender.tag)

case 1:
    print(sender.tag)
case 2:
    print(sender.tag)
case 3:
    print(sender.tag)

default:
    print("Tag was not found")
  }
}