2015-04-30 67 views
1

我一直試圖在單擊按鈕時使用函數中的自定義單元格。並使用該功能更新該特定行中的標籤。在Swift中創建自定義單元格的參考和更新標籤

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 
    } 

} 

回答

2
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 
} 
+0

謝謝,它的工作原理:) – AaoIi

相關問題