2016-12-06 138 views
0

所以我有一个按钮,看起来像在收集观察室厦门国际银行铅笔。集合视图单元格按钮不触发动作

enter image description here

然后,我有这样的代码。

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "groupsCollectionViewCellIdentifier", for: indexPath) as! GroupCollectionViewCell 

    //add action to the edit button 
    cell.editButton.tag = indexPath.row 
    cell.editButton.addTarget(self, action: #selector(GroupsCollectionViewController.editGroupAction), for: .touchUpInside) 

    return cell 
} 

//segue to edit group 
func editGroupAction() { 
    performSegue(withIdentifier: "editGroupSegue", sender: self) 
} 

但每当我点击编辑按钮。什么都没有发生。我不知道缺少什么。

回答

0

你的类分配到小区,并与控制器连接的按钮,也是标识符分配给SEGUE,还可以使用断点检查func被调用或不

,并使用赛格瑞这样

​​
+0

我已经分配给小区课堂上,Segue公司也有一个标识符。 –

+0

你是否尝试断点,然后检查func是否被调用 –

+0

所以在断点中它甚至没有调用函数。点击按钮什么也不做。 –

1

考虑到您的问题,我已经创建了一个演示,其中包含上面提供的细节以及一些细微的更改。
我已修改您的cellForItem方法。我的修改版本如下。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell : GroupCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "groupsCollectionViewCellIdentifier", for: indexPath) as! GroupCollectionViewCell 


    cell.editButton.tag = indexPath.row 
    cell.editButton.addTarget(self, action: #selector(editGroupAction(sender:)), for: .touchUpInside) 

    return cell 
} 


和动作方法写入如下:

func editGroupAction(sender: UIButton) { 
    print("Button \(sender.tag) Clicked") 
} 


从你的代码中,我已经修改了以下内容:
1)UICollectionViewCell对象的申报之路。
2)分配方法#Selector方法到UIButton。并在最后
3)在行动方法我打印按钮标签。

随着上述变化,我得到了正确的结果。我将选定的索引值作为按钮标记,在CONSOL中的代码中进行了分配。我得到的输出如下。

Button 2 Clicked 
Button 3 Clicked 
Button 4 Clicked 

我希望这会为您的要求工作。

0

如果您使用的是iOS 10.Following工作代码

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


      let cell = collectionView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! YourCellClass 
cell.btnJoin.tag = (indexPath as NSIndexPath).row  
cell.btnJoin.addTarget(self, action: #selector(YourViewControllerClassName.doSomething(_:)), for: UIControlEvents.touchUpInside) 
    } 

行动

func doSomething(_ sender: UIButton) { 
      print("sender index",sender.tag) 
    } 
相关问题