我有一个包含带装饰图像的单元格的集合视图,其中包含一个图像视图和一个标签,该标签是我在“详细”控件视图中设置的值,如果未设置,则该值不应出现(如您在图片)。 (顺便说一句我使用核心数据)。

问题在于标签(较小的圆圈),一旦我删除一个单元格或添加另一个单元格,就会出现一些标签,而有些则不会。只有当我重新启动应用程序时,所有内容才会再次显示。

来自collectionview类的相关代码:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString* identifier =@"cell";
    THCollectionEnrtyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    THDiaryEntry *entry = [self.fetchedResultsController objectAtIndexPath:indexPath];
    [cell configureCellForEntry:entry];
    return cell;
}


单元格类中的相关代码:

- (void)configureCellForEntry:(THDiaryEntry *)entry {


    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEEE, MMMM d yyyy"];
   // NSDate *date = [NSDate dateWithTimeIntervalSince1970:entry.date];

   // self.dateLabel.text = [dateFormatter stringFromDate:date];

    self.mainImageView.frame = self.contentView.bounds; //fill it even tho we dont realy need

    if (entry.imageData) {
        self.mainImageView.image = [UIImage imageWithData:entry.imageData];
    } else {
         self.mainImageView.image = [UIImage imageNamed:@"icn_noimage"];
    }



    self.mainImageView.layer.cornerRadius = CGRectGetWidth(self.mainImageView.frame) / 2.0f;
    self.weightLabel.layer.cornerRadius = CGRectGetWidth(self.weightLabel.frame)/ 2.0f;
    self.weightLabel.text = entry.weight;
    if ([self.weightLabel.text isEqualToString:@"n/a"] || [self.weightLabel.text isEqualToString:@""] ) {
        self.weightLabel.hidden = YES;
    }
}




我很确定这与单元的可重用性有关(当我添加一个没有值的单元,然后其他一些具有值的单元没有出现在我的标签上时,我会注意到它),有什么想法如何处理它?

最佳答案

从描述中很难确切知道您的行为。我注意到的一件事是您在某些情况下设置了self.weightLabel.hidden = YES;,但从未将其设置为NO。这可能是重复使用的单元格有时不显示您的weightLabel的原因。

考虑为您的UICollectionViewCell子类实现- (void)prepareForReuse。这将允许您在重用之前将单元格返回到已知的默认状态,并且可以使- (void)configureCellForEntry:(THDiaryEntry *)entry的逻辑更简单。

关于ios - 集合 View 中的可重用项目将无法正确显示自定义单元格 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23895239/

10-09 01:02
查看更多