I Have Create one button , and Two label On tableview Cell , When click on button Then change tableview label text Here is my code
func buttonLike_ww(sender: AnyObject?) {
var tag:NSInteger = sender!.tag;
tag_last_button_clicked = tag;
// found table
let cell = TableViewCell_trandingjive(style:UITableViewCellStyle.Default, reuseIdentifier:"Cell")
cell.label_dislike.text = "20"
cell.label_like.text = "50"
}
func buttonLike_ww(sender: AnyObject?) {
let tag:NSInteger = sender!.tag;
let indexPath = NSIndexPath(forRow: tag, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell.label_dislike.text = "20"
cell.label_like.text = "50"
}
Basically what you do is get the tag from the button (which is the indexPath of the UITableViewCell if I got you right). Then get a reference on the cell and change the labels to the text accordingly.
You made the mistake in your approach that you didn't got a reference on the cell but you created a new one. Also the above code only works if you got only one section. If you got more than one section you need to know not only the row but the section too.
In this case you can go with this approach:
func buttonLike_ww(sender: AnyObject?) {
var position: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
if let indexPath = self.tableView.indexPathForRowAtPoint(position) {
let section = indexPath.section
let row = indexPath.row
let indexPath = NSIndexPath(forRow: row, inSection: section)
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell.label_dislike.text = "20"
cell.label_like.text = "50"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With