错误:我的UILabel显示在一个单元格上,如何修复?

时间:2016-03-24 07:40:18

标签: ios objective-c collectionview

我的UILabel出现在某个单元格上,当我滚动我的UICollectionView时,其他人出现在单元格上但不是全部,如何修复?谢谢

此标签与相同标签的其他单元格混合

  

// 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) {

        }];
    }

2 个答案:

答案 0 :(得分:0)

这是由表格单元格回收引起的,UICollectionView每次都不会创建一个全新的单元格,它会回收旧单元格作为新单元格。一旦在cellForItemAtIndexPath中设置了一个单元格,然后设置相应的数据以使其正确显示其数据,您应该始终重置该单元格的状态

来自documentation

- dequeueReusableCellWithReuseIdentifier:forIndexPath:
  

当要求提供a时,请从数据源对象中调用此方法   集合视图的新单元格。这种方法使现有的队列出局   单元格如果可用或基于类或创建新单元格   您之前注册的nib文件。

     

重要事项您必须使用以下命令注册类或nib文件   registerClass:forCellWithReuseIdentifier:或   registerNib:forCellWithReuseIdentifier:调用它之前的方法   方法

     

如果您为指定的标识符和新单元格注册了一个类   必须创建,此方法通过调用它来初始化单元格   initWithFrame:方法。对于基于nib的单元,此方法加载单元   来自提供的nib文件的对象。如果现有单元格可用   为了重用,此方法调用单元格的prepareForReuse方法   代替。

答案 1 :(得分:0)

这是由于回收单元格而导致的,只是分配表格视图单元格并继续重用单元格

 UITableviwCell *cell = [UITableViewCell alloc]init];
 cell.label.text = ......