单击按钮时,我一直在尝试在功能中使用自定义单元格。并使用该功能更新该特定行中的标签。

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
    }

}

最佳答案

func startcount(sender: AnyObject){
    let button = sender as UIButton
    let indexPath = NSIndexPath(forRow:button.tag inSection:0)
    let cell = tableView.cellForRowAtIndexPath(indexPath) as CardCell

    // update cell
}

关于ios - 在函数中创建自定义单元格的引用并在Swift中更新Label,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29964697/

10-12 05:55