Swift- How to get label value in custom table view cell on button click
我对Swift和Xcode还是很陌生,所以这个问题对于某些人来说可能很基础。
我在自定义表格视图单元格中有一个按钮和一个标签。我正在尝试检测到单击按钮时更改标签文本的值。
当前,我正在设置
1 2 | cell.btn.tag = indexPath.row cell.btn.addTarget(self, action:"btn_click:", forControlEvents: UIControlEvents.TouchUpInside) |
如何访问与单击按钮相同的单元格中包含的标签,以便可以更改标签文本的值?我可以使用
您不需要使用标签来实现您的目标,在您的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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" } } |
您可以在Interface Builder中手动设置按钮的操作和标签的出口,而无需在代码中进行设置。
然后,当您在单元格中的按钮内点击时,标签仅在单元格内被更改。
希望对您有所帮助。