简体   繁体   English

collectionView单元格中的按钮选择器

[英]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.

相关问题 将动画添加到collectionview单元内的按钮 - Adding animation to a button inside a collectionview cell CollectionView 内的按钮重复第一个单元格信息 - Button inside the CollectionView repeating the first cell information 将目标添加到tableview单元格内的collectionview单元格的按钮 - Add target to button of collectionview cell inside tableview cell tableview单元格内的Collectionview不会增加collectionView的高度 - Collectionview inside tableview cell not increasing height of collectionView CollectionView 中的 ImageView 和 Button - ImageView and Button inside CollectionView CollectionView 中的按钮不可点击 - Button inside CollectionView not clickable UITableView内的CollectionView单元重复 - CollectionView cell inside UITableView is repeating collectionview在tableview单元格内重复 - collectionview are repeating inside a tableview cell 当点击collectionView单元格内的按钮时,如何获取表格视图行并从位于tableViewCell内的collectionView推送到viewController - how to get table view row and push to viewController from collectionView that's inside a tableViewCell when tapped button inside collectionView cell 如何在tableview单元格内的collectionview单元格的按钮单击时获取索引 - How to get index on button click of collectionview cell which is inside tableview cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM