Reputation: 331
I'm very new to Swift and Xcode so this question may sound elementary to some.
I have a button and a label in a custom table view cell. I'm trying to change the value of the label text when the button click is detected.
Currently, I'm setting the button.tag = indexPath.row
(as shown in the code below) and I'm catching it in the btn_click
function.
cell.btn.tag = indexPath.row
cell.btn.addTarget(self, action: "btn_click:", forControlEvents: UIControlEvents.TouchUpInside)
How can I access the label contained within the same cell as the button clicked so I can change the value of the label text? Can I use indexPath.row
to return the correct label object?
Upvotes: 2
Views: 4514
Reputation: 23459
You don't need to use the tag to accomplish your goal, in your CustomTableViewCell
class you have to set an action to the button and a outlet to the label inside the cell, something like this :
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var label: UILabel!
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
}
// action to tap the button and change the label text
@IBAction func tappedButton(sender: AnyObject) {
self.label.text = "Just Clicked"
}
}
You can the manually set the action for the button and the outlet for the label in the Interface Builder, you don't need to set in code.
Then, when you make a tap inside the button in the cell the label it's changed only inside the cell.
I hope this help you.
Upvotes: 1