0

My UILabel appears on some cell and times when I scroll my UICollectionView others appear on the cell but not all, how to fix? Thank you

This label mixes with the same label other cell

// cellForItemAtIndexPath

self.date = [NSDate dateWithTimeIntervalSince1970:[[searchResult objectForKey:@"drawDate"] intValue]];
                    self.dateFormatter = [[NSDateFormatter alloc] init];
                    [self.dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]]];
                    [self.dateFormatter setDateStyle:NSDateFormatterShortStyle];

                cell3.titleContest.hidden = YES;
                cell3.titleContest.alpha = 0;
                //self.dateContest.text = @"";

                NSTimer *timer = [[NSTimer alloc] init];

                timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];

// countDownTimer

- (void)updateCounter:(NSTimer *)tmr
        {
            NSTimeInterval iv = [self.date timeIntervalSinceNow];
            int d = iv / 86400;
            int h = (iv / 3600) - (d * 24);
            int m = (iv - (h + d * 24) * 3600) / 60;
            int s = lroundf(iv) % 60;
            cell3.titleContest.text = [NSString stringWithFormat:@"%02d:%02d:%02d:%02d", d, h, m, s];
            if (d + h + m + s <= 0

) {
                [tmr invalidate];
            }

        [UIView animateWithDuration:0.5 animations:^{
            cell3.titleContest.hidden = NO;
            cell3.titleContest.alpha = 1;
        } completion:^(BOOL finished) {

        }];
    }
victor bill
  • 209
  • 2
  • 15

2 Answers2

0

This is caused by table cell recycling, the UICollectionView does not create a completely new cell each time, it will recycle an old cell to use as the new one. you should always reset the state of a cell once it has been dequed in cellForItemAtIndexPath then set the appropriate data to make it show its data correctly

from the documentation:

- dequeueReusableCellWithReuseIdentifier:forIndexPath:

Call this method from your data source object when asked to provide a new cell for the collection view. This method dequeues an existing cell if one is available or creates a new one based on the class or nib file you previously registered.

IMPORTANT You must register a class or nib file using the registerClass:forCellWithReuseIdentifier: or registerNib:forCellWithReuseIdentifier: method before calling this method.

If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithFrame: method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse method instead.

Fonix
  • 11,447
  • 3
  • 45
  • 74
0

this is caused due to recycling cells in order to over come that just alloc the table view cell and continue with out reusing the cells

 UITableviwCell *cell = [UITableViewCell alloc]init];
 cell.label.text = ......
IOS DEV
  • 126
  • 5