【发布时间】: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