如何在不操作单元格界面的情况下在UITableViewCell中

如何在不操作单元格界面的情况下在UITableViewCell中

本文介绍了如何在不操作单元格界面的情况下在UITableViewCell中点按单元格的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自定义单元格有2个按钮,并且每个自定义单元格也执行segue.

My custom cell have 2 buttons, and each custom cell also perform a segue.

segue功能正常,问题出在我的按钮从未触发过的事实.如果单击它们,则会触发segue,但不会触发按钮.如何克服这个问题?

The segue is functionning properly, the problem come from the fact that my buttons are never fired.If I click on them then the segue is fired, but not the button.How to overcome this?

下面是如何定义按钮动作的代码:

Here goes the code of how the button's actions are defined :

在表格视图控制器中

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(CCELL.WISHCELL, forIndexPath: indexPath) as! WishCell
    let row = indexPath.row
    let product = products.elements[row]


    // Setup the 2 cell buttons
    cell.removeButton.tag = indexPath.row
    cell.addToCartButton.tag = indexPath.row
    cell.removeButton.addTarget(self, action: "removeFromWishList:", forControlEvents: .TouchUpInside)
    cell.addToCartButton.addTarget(self, action: "addToCart", forControlEvents: .TouchUpInside)

选择器的目标功能有效,我没有错误.按钮是永远不会触发的.

如果不是那么琐碎,需要更多代码,请问我!

If it's not that trivial and more code is needed just ask me!

推荐答案

到目前为止找到的最佳解决方案:

The best solution I found so far:

override func tableView(tableView: UITableView,     editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .Destructive, title: "Delete") { (action, indexPath) in
    // delete item at indexPath
}
  let share = UITableViewRowAction(style: .Normal, title: "Disable") { (action, indexPath) in
    // share item at indexPath
}

  share.backgroundColor = UIColor.blueColor()

  return [delete, share]
}

将我的操作作为滑动按钮,它更具"iOS"风格

Having my actions as swipe buttons, it's more "iOS" style

这篇关于如何在不操作单元格界面的情况下在UITableViewCell中点按单元格的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:00