2016-11-17 51 views
0

这里是我的代码的UILabel添加到特定indexpath在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; 
} 

我想只有索引路径零添加标签。作为标签出现在其他指数路径还 我得到的问题..

回答

0

在你的情况,如果条件里面,你还可以添加:

label.tag = 1000; // or other number 

而且,里面还有补充:

[cell.contentView viewWithTag:1000].hidden = YES; 

问题是indexPath.item == 0处的单元格可能会重用于其他索引路径,并且它仍然可见。

希望它是有道理的。

+0

感谢您的回复 –

+0

@SunilKumar不客气!它有用吗?如果是 - 请接受我的回答。 – ppalancica