【问题标题】:iOS cell add target not working properly?iOS 单元格添加目标无法正常工作?
【发布时间】:2016-05-03 09:59:24
【问题描述】:

在我的应用程序中,我在 UICollectionView cellForRowAtIndexpath 中编写了以下代码,例如 [CollectionView Cell is a custom cell]

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    CollectionViewCell *cell = (CollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"localMusicCell" forIndexPath:indexPath];

[[cell.downImageButton viewWithTag:indexPath.item] addTarget:self action:@selector(downImgClicked:) forControlEvents:UIControlEventTouchUpInside];

return cell;
}

目标方法如下:

-(void)downImgClicked:(UIButton*)button{

}

UICollectionView 中有四个项目,但是对于第一个项目,只有这个目标方法被调用,其余的甚至没有触发为什么?

【问题讨论】:

  • 我想你想要[cell.downImageButton addTarget:self action:@selector(downImgClicked:) forControlEvents:UIControlEventTouchUpInside];
  • @Nishant 我还需要传递 indexPath.item 目标吗?怎么样?
  • 在目标方法中使用cell.downImageButton.tag = indexPath.item; AND THEN,-(void)downImgClicked:(UIButton*)button{ int indexPathItem = button.tag; }
  • 感谢 Nishant 现在可以正常工作了,我的代码有什么问题?
  • [cell.downImageButton viewWithTag:indexPath.item] 返回downImageButton 的子视图,并且目标被添加到其中(可能为零)。您需要将目标添加到按钮本身。您可以查看viewWithTag: 方法的文档。

标签: ios objective-c target


【解决方案1】:

尝试其他方式

cell.downImageButton.tag = indexPath.item;
[cell.downImageButton addTarget:self action:@selector(downImgClicked:) forControlEvents:UIControlEventTouchUpInside];

调用方法喜欢

-(void)downImgClicked:(UIButton*)button{
  NSLog (@"selected index ==%@",button.tag);
 }

【讨论】:

  • 没问题的兄弟,有时用户交互可能无法访问
【解决方案2】:

试试这个方法

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {


        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! AllCollectionViewCell

        cell.playBtn.tag = indexPath.row
        cell.playBtn.addTarget(self, action: Selector("buttonAction:"), forControlEvents: .TouchUpInside)

        return cell

    }

目标方法:

func buttonAction(sender:UIButton!) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewControllerWithIdentifier("SampleViewController") as! PlayViewController
        self.navigationController?.pushViewController(controller, animated: true)
    }

它对我有用,希望它有帮助

【讨论】:

    【解决方案3】:
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    
         CollectionViewCell *cell = (CollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"localMusicCell" forIndexPath:indexPath];
    
        cell.downImageButton.tag =indexPath.row;
        [cell.downImageButton  addTarget:self action:@selector(downImgClicked:) forControlEvents:UIControlEventTouchUpInside];
    
        return cell;
    
    }
    

    目标方法如下:

    -(void)downImgClicked:(UIButton*)button
    {
     long selectedBtnRow;
    
     selectedBtnRow = button.tag;
    
     NSLog (@"selected index ==>%ld",selectedBtnRow);
    } 
    

    【讨论】:

      猜你喜欢
      • 2019-03-05
      • 2013-08-19
      • 1970-01-01
      • 2019-02-21
      • 2018-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多