Reputation: 16128
I have a basic UICollectionView that if I scroll will "redraw" the label on top of the cells,
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor=[UIColor greenColor];
UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, cell.bounds.size.width, 40)];
[cell.contentView addSubview:title];
//NSString *titleLbl = [NSString stringWithFormat:@"i = %d", indexPath.row];
title.text = [self.arraya objectAtIndex:indexPath.row];
return cell;
}
How to fix it so it refreshes the proper cell after scrolling? Cheers
Upvotes: 0
Views: 1681
Reputation: 36
When you do this:
UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, cell.bounds.size.width, 40)];
[cell.contentView addSubview:title];
You're creating a new title every time and adding it to your contentView. Instead, try adding a tag to your UILabel and changing your existing UILabel's text, like so:
UILabel *title = (UILabel *)[cell.contentView viewWithTag:SOME_NUMBER];
if (!title) {
title = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, cell.bounds.size.width, 40)];
title.tag = SOME_NUMBER;
[cell.contentView addSubview:title];
}
title.text = @"your new text"
Upvotes: 2