如何在Swift中的UITableViewCell上添加带有click事件的按钮?

Kem*_*ğlu 15 uitableview ios swift

在我的主页面中,我为UITableViewCell创建了一个xib文件.我正在从该xib文件中加载单元格,并且工作正常.

在细胞内部,我有一些标签和按钮.我的目标是通过单击单元格上的按钮来更改标签.

我的代码喜欢以下

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
    }
}
Run Code Online (Sandbox Code Playgroud)

这是代码后面的xib文件,如.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) {



  }
}
Run Code Online (Sandbox Code Playgroud)

当我点击Cell内部的fav按钮时.我想将标签文本的时间更改为任何内容.

当我点击fav按钮时,事件将转到SepetCell.swift favoriteClicked(发送者:UIButton)函数.

所以,如果我尝试调用:SepetViewController().favorite(sender.tag)

它将进入内部

func favorite(tag: Int) {

      sepetTable.reloadData()

    }
Run Code Online (Sandbox Code Playgroud)

但是sepetTable在它去的时候是零.我认为这是因为当我调用这个SepetViewController().favorite(sender.tag)函数时.它首先创建SepetViewController类.因此,由于未设置对象,因此它将变为null.

我怎样才能达到sepetTable或解决此问题的最佳方法.

谢谢.

raf*_*raf 53

解决这个问题的流行模式是闭包和代表.如果你想使用闭包,你会做这样的事情:

final class MyCell: UITableViewCell {
    var actionBlock: (() -> Void)? = nil
Run Code Online (Sandbox Code Playgroud)

然后

    @IBAction func didTapButton(sender: UIButton) {
        actionBlock?()
    }
Run Code Online (Sandbox Code Playgroud)

然后在你的tableview委托:

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

    let cell = tableView.dequeueReusableCellWithIdentifier("MyCellIdentifier") as? MyCell
    cell?.actionBlock = {
       //Do whatever you want to do when the button is tapped here
    }
Run Code Online (Sandbox Code Playgroud)

一种流行的替代方法是使用委托模式:

    protocol MyCellDelegate: class {
        didTapButtonInCell(_ cell: MyCell)
    }

    final class MyCell: UITableViewCell {
        weak delegate: MyCellDelegate?
Run Code Online (Sandbox Code Playgroud)

然后

    @IBAction func didTapButton(sender: UIButton) {
        delegate?.didTapButtonInCell(self)
    }
Run Code Online (Sandbox Code Playgroud)

..现在在你的视图控制器中:

然后在你的tableview委托:

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

    let cell = tableView.dequeueReusableCellWithIdentifier("MyCellIdentifier") as? MyCell
    cell?.delegate = self
Run Code Online (Sandbox Code Playgroud)

并添加协议的一致性,如下所示:

extension MyViewController: MyCellDelegate {
    didTapButtonInCell(_ cell: MyCell) {
       //Do whatever you want to do when the button is tapped here
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

  • 好吧,我通过一些尝试找到它:)我对代码做了一些更改,如:var onFavButtonTapped:((Int) - > Void)?= nil****如果让onButtonTapped = self.onButtonTapped {onButtonTapped(3)}****cell!.onButtonTapped = {(x:Int)in println(x)} (3认同)