Ios collectionView单元格内按钮的选择器
这让我快发疯了,我已经读了好几个小时了,什么都试过了,但是这个按钮选择器不能工作。这应该不难 在CellForItemAt内,我设置了按钮标签,并尝试调用按钮Ios collectionView单元格内按钮的选择器,ios,swift,selector,Ios,Swift,Selector,这让我快发疯了,我已经读了好几个小时了,什么都试过了,但是这个按钮选择器不能工作。这应该不难 在CellForItemAt内,我设置了按钮标签,并尝试调用按钮 cell.deleteCellButton.tag = indexPath.item cell.deleteCellButton.addTarget(self, action: #selector(deleteCellButtonTapped(sender:)), for: UIControlEvents.touchUpInside)
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()
}
假设您使用的是Swift 3,并且
func deleteCellButtonTapped(发件人:UIButton!)
在同一个类中:
addTarget(self, action: #selector(deleteCellButtonTapped(sender:)), for: .touchUpInside)
工作正常。假设您使用的是Swift 3,并且
func deleteCellButtonTapped(发件人:UIButton!)
属于同一类:
addTarget(self, action: #selector(deleteCellButtonTapped(sender:)), for: .touchUpInside)
很好。从它的类中引用选择器方法对我很有效 您可以做的是访问选择器方法,它的前缀是类名 我假设您的类名是
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")
}
}
希望这很好参考它的类中的选择器方法对我有用 您可以做的是访问选择器方法,它的前缀是类名 我假设您的类名是
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")
}
}
希望这一切都很好#选择器(self.deleteCellButtonTapped(发件人:))对我有效。#选择器(self.deleteCellButtonTapped(发件人:)对我有效。