如何在Swift中的UITableViewCell上点击事件添加按钮?

问题描述:

在我的主页中,我为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或什么是解决此问题的最佳方法。

谢谢。

我会在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