Вопрос

I have a basic UICollectionView that if I scroll will "redraw" the label on top of the cells,

enter image description here

- (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

Это было полезно?

Решение

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"
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top