Ios 在函数中创建自定义单元格的引用并在Swift中更新标签
我一直试图在单击按钮时使用函数中的自定义单元格。并使用函数更新该特定行中的标签Ios 在函数中创建自定义单元格的引用并在Swift中更新标签,ios,swift,Ios,Swift,我一直试图在单击按钮时使用函数中的自定义单元格。并使用函数更新该特定行中的标签 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = myTableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CardCell
cell.startcount.tag = indexPath.row
cell.startcount.addTarget(self, action: "startcount:", forControlEvents:UIControlEvents.TouchUpInside)
}
这是我正在使用的函数
func startcount(sender: AnyObject){
// create a reference of CardCell
// update the counter like cell.textcount.text = "\(counter)"
// in the specific row
}
我的自定义单元格类:
import UIKit
class CardCell: UITableViewCell {
@IBOutlet weak var textcount: UILabel!
@IBOutlet weak var startcount: 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
}
}
适用于Swift 4及以上版本
func startCount(sender : AnyObject) {
let button = sender as! UIButton
let indexPath = IndexPath(row:button.tag ,section:0)
let cell = tableView.cellForRow(at: indexPath) as! CardCell
// update cell
}
适用于Swift 4及以上版本
func startCount(sender : AnyObject) {
let button = sender as! UIButton
let indexPath = IndexPath(row:button.tag ,section:0)
let cell = tableView.cellForRow(at: indexPath) as! CardCell
// update cell
}