2015-05-07 215 views
6

在我的主頁中,我爲UITableViewCell創建了一個xib文件。我從該xib文件加載單元格,並且工作正常。如何在Swift中的UITableViewCell上點擊事件添加按鈕?

單元格內有一些標籤和按鈕。我打算通過點擊單元格上的按鈕來更改標籤。

我的代碼如下喜歡

import UIKit 


class SepetCell: UITableViewCell{ 

    @IBOutlet var barcode: UILabel! 
    @IBOutlet var name: UILabel! 
    @IBOutlet var fav: UIButton! 
    @IBOutlet var strep: UIStepper! 
    @IBOutlet var times: UILabel! 



    @IBAction func favoriteClicked(sender: UIButton) { 
     println(sender.tag) 
     println(times.text) 

     SepetViewController().favorite(sender.tag) 



    } 
    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我背後廈門國際銀行文件。

在主頁的代碼下面喜歡:

import UIKit 
import CoreData 

class SepetViewController: UIViewController, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate { 

    @ 
    IBOutlet 
    var sepetTable: UITableView! 
    var barcodes: [CART] = [] 

    let managedObjectContext = (UIApplication.sharedApplication().delegate as!AppDelegate).managedObjectContext 

    override func viewWillAppear(animated: Bool) { 
    if let moc = self.managedObjectContext { 


     var nib = UINib(nibName: "SepetTableCell", bundle: nil) 
     self.sepetTable.registerNib(nib, forCellReuseIdentifier: "productCell") 
    } 
    fetchLog() 
    sepetTable.reloadData() 
    } 

    func fetchLog() { 

    if let moc = self.managedObjectContext { 
     barcodes = CART.getElements(moc); 
    } 
    } 



    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) - > Int { 
    return self.barcodes.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell { 


    let cell = tableView.dequeueReusableCellWithIdentifier("productCell") as ? SepetCell 


    if cell == nil { 
     println("cell nil") 

    } 



    let product: CART 
    product = barcodes[indexPath.row] 



    cell!.barcode ? .text = product.barcode 
    cell!.name ? .text = product.name 
    cell!.fav.tag = indexPath.row 

    return cell! 
    } 

    func favorite(tag: Int) { 



    } 
} 

當我點擊的細胞的內部收藏的按鈕。例如,我想將時間標籤文本更改爲任何內容。

當我點擊fav按鈕時,事件將轉到SepetCell.swift favoriteClicked(sender:UIButton)函數。

所以,如果我打電話: SepetViewController()最喜歡的(sender.tag)

它會去

func favorite(tag: Int) { 

     sepetTable.reloadData() 

    } 

內部,而且sepetTable爲零當它去那裏。我認爲這是因爲當我調用這個SepetViewController()。favorite(sender.tag)函數。它首先創建SepetViewController類。因爲沒有設置對象,所以它變爲空。

我該如何達到sepetTable或什麼是解決此問題的最佳方法。

謝謝。

回答

28

我會在Cell類中定義一個封閉:

class SepetCell: UITableViewCell{ 
    var onButtonTapped : (() -> Void)? = nil 

然後

@IBAction func favoriteClicked(sender: UIButton) { 
     println(sender.tag) 
     println(times.text) 
     if let onButtonTapped = self.onButtonTapped { 
      onButtonTapped() 
     } 
    } 

然後在你的tableview委託:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("productCell") as ? SepetCell 
    cell.onButtonTapped = { 
     //Do whatever you want to do when the button is tapped here 
    } 

希望這有助於!

+0

非常感謝。它爲我工作。如果我想從SepetCell發送someView到tableView,我應該怎麼做?例如,我有一個strepper,當我點擊它時,我想更改標籤的值 –

+2

ok我發現它的一些嘗試:) 我對代碼做了一些更改: var onFavButtonTapped :((Int) - >無效)? =零**** 如果讓onButtonTapped = self.onButtonTapped { onButtonTapped(3) } ****細胞.onButtonTapped =!{(X:智力)在 的println(X) } –

+0

大到聽!。順便說一句,如果你發現我的答案有用,請考慮upvoting :) – raf

相關問題