2017-06-08 6 views
0

Ich habe eine Schaltfläche auf der collectionviewcell hinzugefügt, und sobald Benutzer auf diese Schaltfläche klickt, ruft es die folgende Methode, aber uviCollectionViewCell gibt Null zurück. gleich OhrenmarkeZugriff auf CollectionViewCell

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(collectionView == productCollectionView) 
    { 
     __weak ProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; 

     cell.addBtnOutlet.tag = indexPath.row; 
     [cell.addBtnOutlet addTarget:self 
         action:@selector(collectionViewCellAddButtonPressed:) 
       forControlEvents:UIControlEventTouchUpInside]; 
    } 
    return cell; 
} 

- (IBAction)collectionViewCellAddButtonPressed:(UIButton *)button{ 
    NSLog(@"Add Button Clicked"); 
    // the following is nil 
    UICollectionViewCell *uviCollectionCell = [self.productCollectionView cellForItemAtIndexPath:[NSIndexPath indexPathWithIndex:0]]; 
} 
+1

[NSIndexPath indexPathForRow: button.tag insection: 0] für vollständige Code '[self.productCollectionView cellForItemAtIndexPath: [NSIndexPath indexPathForRow: button.tag insection: 0 ]]; ' –

+0

@ anbu-carthik: Danke für die Bearbeitung der Antwort Ich habe Ihre Bearbeitung beibehalten, während ich nicht sicher bin, was OP Anforderung ist daher eine Notiz so gut gehalten –

+0

@ Anbu.Karthik, vielen Dank, es hat funktioniert. Ich habe den Abschnitt nicht hinzugefügt, ich dachte, wenn ich es nicht aufstelle, wird es Null als Standard zuweisen. Bitte geben Sie Ihren Code als Antwort ein und ich werde ihn akzeptieren. – hotspring

Antwort

1

Alles, was Sie brauchen, ist,

Wenn Sie eine Zelle mit Indexpfad müssen dann als Karthik sagte

UICollectionViewCell *uviCollectionCell = [self.collectionView.dataSource collectionView:self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:button.tag inSection:0]]; 

, wenn Sie immer eine Zelle an der Indexpfad brauchen 0,0 dann verwenden

UICollectionViewCell *uviCollectionCell = [self.collectionView.dataSource collectionView:self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; 

Ich hoffe, es hilft.

1

Wenn Sie möchten, ohne Tag zu tun, dann

- (IBAction)collectionViewCellAddButtonPressed:(UIButton *)button{ 
    NSLog(@"Add Button Clicked"); 
    UICollectionViewCell *uviCollectionCell = (UICollectionViewCell *)button.superview; 

    // if this is return nil then use like below as i am not sure it will retuen contentview for superview 
    UICollectionViewCell *uviCollectionCell1 = (UICollectionViewCell *)button.superview.superview; 
} 
Verwandte Themen