2016-05-03 8 views
1

В моем приложении я написал следующий код в UICollectionViewcellForRowAtIndexpath как этот [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; 
} 

и целевой метод, как это:

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

} 

И есть четыре пункта в UICollectionView, но для первый элемент только этот целевой метод называется, и для остальных из них его даже не запускают почему?

+2

я думаю, что вы хотите '[cell.downImageButton addTarget: само действие: @selector (downImgClicked :) forControlEvents: UIControlEventTouchUpInside] ; ' – Nishant

+0

@Nishant Мне нужно передать indexPath.item также цель? как? – Group

+1

Используйте 'cell.downImageButton.tag = indexPath.item;' AND THEN в целевом методе '- (void) downImgClicked: (UIButton *) button { int indexPathItem = button.tag; } ' – Nishant

ответ

3

попробовать альтернативный способ

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

вызов метода как

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

В чем проблема с моим кодом? – Group

+0

ничего страшного брата, иногда userinteaction not reacheable может быть –

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; 

} 

и целевого метода, как это:

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

selectedBtnRow = button.tag; 

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

попробовать этот способ,

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) 
    } 

его работы для меня, надеюсь, что его полезно