2017-02-20 35 views
1

这使我疯了,我一直在阅读SO几个小时,并尝试了一切,不能让这个按钮选择器工作。这应该不难。收藏按钮选择器查看单元格

Inside CellForItemAt我已经设置了按钮标签并尝试调用按钮。

cell.deleteCellButton.tag = indexPath.item 
cell.deleteCellButton.addTarget(self, action: #selector(deleteCellButtonTapped(sender:)), for: UIControlEvents.touchUpInside) 

我试过(_ :)“deleteCellButtonTapped:”和其他任何数量的括号组合和我仍然得到无法识别的选择。我不知道为什么自动完成建议(发件人:)我从来没有见过这个。

那么我的按钮功能:

func deleteCellButtonTapped(sender: UIButton!) { 
    self.packArray.remove(at: sender.tag) 
     print(packArray.count) 
    self.outerCollectionView.deleteItems(at: [IndexPath(item: sender.tag, section: 0)]) 

    self.outerCollectionView.reloadData() 
    self.outerCollectionView.layoutIfNeeded() 
} 
+0

#selector(自deleteCellButtonTapped(发件人:))为我工作。 – Anuraj

回答

0

假设你正在使用斯威夫特3,和func deleteCellButtonTapped(sender: UIButton!)是在同一类:

addTarget(self, action: #selector(deleteCellButtonTapped(sender:)), for: .touchUpInside) 

工作正常。

0

参考选择器方法从它的类适用于我。

你可以做的是接入选择器方法,它的前缀是类名称

我假设你的班级名称是MyClassViewController。你代码如下:

class MyClassViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 

    .... // Other implementation methods 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     ....// create cell object and dequeue 

     cell.deleteCellButton.addTarget(self, action: #selector(MyClassViewController.deleteCellButtonTapped(_:)), for: .touchUpInside) 
     return cell 
    } 

    func deleteCellButtonTapped(_ sender: Any) { 
     // your method implementation 
      print("Selector method called") 
    } 
} 

希望这会工作得很好