[英]selector for button inside collectionView cell
This is driving me nuts, I've been reading SO for hours and tried everything and cant get this button selector to work. 这让我疯了,我已经读了好几个小时并试了一切,不能让这个按钮选择器工作。 This shouldn't be difficult. 这应该不难。
Inside CellForItemAt i have set the button tag and try call the button. 在CellForItemAt内部我设置了按钮标签并尝试调用按钮。
cell.deleteCellButton.tag = indexPath.item
cell.deleteCellButton.addTarget(self, action: #selector(deleteCellButtonTapped(sender:)), for: UIControlEvents.touchUpInside)
I've tried (_:), "deleteCellButtonTapped:", and any other number of parenthesis combinations and i still get unrecognised selector. 我试过(_ :),“deleteCellButtonTapped:”,以及任何其他数量的括号组合,我仍然得到无法识别的选择器。 i don't know why autocomplete recommends (sender:) I've never seen this before. 我不知道为什么autocomplete建议(发送者:)我以前从未见过这个。
then my button function: 然后我的按钮功能:
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()
}
Assuming you're using Swift 3, and func deleteCellButtonTapped(sender: UIButton!)
is in the same class: 假设您正在使用Swift 3,并且func deleteCellButtonTapped(sender: UIButton!)
位于同一个类中:
addTarget(self, action: #selector(deleteCellButtonTapped(sender:)), for: .touchUpInside)
works fine. 工作正常。
Refering the selector method from it's class works for me. 从它的类中引用选择器方法对我有用。
What you can do is access selector method prefixing by it's class name . 你可以做的是访问选择器方法前缀为它的类名 。
I assume your class name is MyClassViewController
. 我假设你的类名是MyClassViewController
。 And you code will look like: 你的代码看起来像:
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")
}
}
Hope this works fine 希望这很好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.