2016-11-17 1 views
0

Hier ist mein Codehinzufügen UILabel zu einem bestimmten indexPath in UICollectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 
    return 10; 
} 

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    CollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"dfdsfs" forIndexPath:indexPath]; 

    if (indexPath.item == 0) { 
     UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 50)]; 
     label.text=[NSString stringWithFormat:@"this is item %ld", (long)indexPath.item]; 
     [cell.contentView addSubview:label]; 
     cell.contentView.backgroundColor = [UIColor greenColor]; 

     NSLog(@"im @ zero"); 

    } else { 
     NSLog(@"im here %ld", (long)indexPath.item); 
     cell.contentView.backgroundColor = [UIColor yellowColor]; 

    } 

    return cell; 
} 

I-Label will bei Indexpfad Null nur hinzugefügt werden. Ich erhalte Problem, da das Label auch in anderen Indexpfade erscheint ..

Antwort

0

In Ihrem Fall in der, wenn die Bedingung, können Sie auch hinzufügen:

label.tag = 1000; // or other number 

Und fügen, innen sonst:

Das Problem war, dass die Zelle bei indexPath.item == 0 für die anderen Indexpfade wiederverwendet werden kann, und es ist immer noch sichtbar.

Ich hoffe, es macht Sinn.

+0

danke für die Antwort –

+0

@SunilKumar Gern geschehen! Hat es funktioniert? Wenn ja - bitte akzeptiere meine Antwort. – ppalancica

Verwandte Themen