如何访问自定义单元格元素

时间:2016-02-19 03:44:01

标签: ios swift

有什么方法可以从我的viewController.swift文件中访问自定义单元格中定义的按钮和标签?我想访问所有其他按钮的文本,并在“handleButtonPressed”函数中设置“plrAvg”的文本(请参阅下面代码中的函数)。

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")
    }
  }
}

1 个答案:

答案 0 :(得分: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")
  }
}