How to tap cell's buttons in UITableViewCell without actionning the cell's segue

隐身守侯 提交于 2019-12-25 03:53:45

问题


My custom cell have 2 buttons, and each custom cell also perform a 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 :

In the table view controller

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)

the functions targette by the selectors are valid, I have no error. Buttons are just never fired.

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


回答1:


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]
}

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



来源:https://stackoverflow.com/questions/33494776/how-to-tap-cells-buttons-in-uitableviewcell-without-actionning-the-cells-segue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!