2016-05-03 4 views
1

私はこのようなこの[CollectionView Cell is a custom cell]iOSセルの追加ターゲットが正しく動作していませんか?私のアプリで

- (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; 
} 

とターゲット・メソッドと同様UICollectionViewcellForRowAtIndexpathに次のコードを書いている:

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

} 

そして4つのUICollectionView内の項目が、用があります最初の項目はこのターゲットメソッドだけが呼び出され、残りの部分はなぜトリガーされないのでしょうか?

+2

私はしたいと思います '[cell.downImageButton addTarget:自己アクション:@selector(downImgClicked :) forControlEvents:UIControlEventTouchUpInside] ; ' – Nishant

+0

@Nishant私はindexPath.itemもターゲットに渡す必要がありますか?どうやって? – Group

+1

'cell.downImageButton.tag = indexPath.item;'とそれ以降はターゲットメソッドで ' - (void)downImgClicked:(UIButton *)ボタン{ int indexPathItem = button.tag; } ' – Nishant

答えて

3

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

私のコードの問題は何ですか? – Group

+0

何の問題もありませんが、時にはユーザーの反応が邪魔にならないことがあります –

2
- (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; 

} 

と、このようなターゲットメソッドのような

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

呼び出し方法別の方法を試してみてください。

-(void)downImgClicked:(UIButton*)button 
{ 
long selectedBtnRow; 

selectedBtnRow = button.tag; 

NSLog (@"selected index ==>%ld",selectedBtnRow); 
} 
3

0、この方法を試してみてください
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) 
    } 

その私のために働いて、期待しその有用

関連する問題