iOS collectionView Cell可以重复使用。为什么单元格的backgroundColor很好,当我添加徽章时,出现徽章错误?

时间:2019-01-14 07:55:11

标签: ios objective-c

calendar.gif

这是我的@ {eventTime:eventCount}:

LYQMemoVC.m:196 dic:{     “ 2019-01-12”:“ 2”,     “ 2019-02-20”:“ 1”,     “ 2019-01-13”:“ 1”,     “ 2019-02-10”:“ 1”,     “ 2019-01-14”:“ 1” }

问题: 事件不是2019年2月9日,但单元格有徽章...因为同一地点将有徽章。不是事件,而是出现徽章。

单元格的backgroundColor很好,但是徽章错误。

(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

NSString *identifier = [self.cellDic objectForKey:[NSString stringWithFormat:@"%@", indexPath]];

if (identifier == nil) {
    identifier = [NSString stringWithFormat:@"ALCalendarCell%@", [NSString stringWithFormat:@"%@", indexPath]];
    [_cellDic setValue:identifier forKey:[NSString stringWithFormat:@"%@", indexPath]];

    [self registerClass:[ALCalendarCell class]  forCellWithReuseIdentifier:identifier];
}
    ALCalendarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];

if(indexPath.section == 0) {
    cell.weekDay = self.titles[indexPath.row];
} else {
    ALCalendarDate *date = self.dates[indexPath.row];
    cell.date = date;


    NSString *dateString;
    if (date.isLastMonth) { 
        dateString = [[ALCalendarHelper lastYearAndMonth:self.yearAndMonth] stringByAppendingFormat:@"-%02zd",date.date.integerValue];
    } else if (date.isNextMonth) { 
        dateString = [[ALCalendarHelper nextYearAndMonth:self.yearAndMonth] stringByAppendingFormat:@"-%02zd",date.date.integerValue];
    } else { 
        dateString = [self.yearAndMonth stringByAppendingFormat:@"-%02zd",date.date.integerValue];
    }

if ([self.config.heightlightDates containsObject:dateString]) {
            cell.backgroundColor = self.config.hl_backgroundColor;
            cell.layer.cornerRadius = self.config.hl_backgroundCornerRadius.floatValue;
            cell.dateLabel.textColor = self.config.hl_textColor;
            [self.config.eventDictionary enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
                if ([key isEqualToString:dateString]) {
                    cell.badge.badgeValue = [obj intValue];
                    cell.badge.backgroundColor = [UIColor lightGrayColor];
                    cell.badge.hidden = NO;
                }
            }];
        }
        if ([self.config.selectedDates containsObject:dateString]) {
            cell.backgroundColor = self.config.sel_backgroundColor;
            cell.layer.cornerRadius = self.config.sel_backgroundCornerRadius.floatValue;
            cell.dateLabel.textColor = self.config.sel_textColor;
            [self.config.eventDictionary enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
                if ([key isEqualToString:dateString]) {
                    cell.badge.badgeValue = [obj intValue];
                    cell.badge.backgroundColor = Config_Main_Color;
                    cell.badge.hidden = NO;
                }
            }];
        }
}
return cell;

}

2 个答案:

答案 0 :(得分:0)

如果我的理解正确,那么您的徽章将在单元“重复使用”中保持不变。 您要根据日期隐藏和显示徽章。

在您的cellForItemAtIndexPath函数中,检查以下内容:

if ([key isEqualToString:dateString]) {
  cell.badge.badgeValue = [obj intValue];
  cell.badge.backgroundColor = Config_Main_Color;
  cell.badge.hidden = NO;
}

,最后一行取消隐​​藏您的cell.badge。您实际上再也不会隐藏您的徽章。而且由于您正在重用单元格,因此没有默认值

您的代码可能看起来应该类似于以下内容(复制到两个if语句):

if ([key isEqualToString:dateString]) {
  cell.badge.badgeValue = [obj intValue];
  cell.badge.backgroundColor = Config_Main_Color;
  cell.badge.hidden = NO;
}
else {
  cell.badge.badgeValue = 0;
  cell.badge.hidden = YES;
}

答案 1 :(得分:0)

首先:您是否有理由在视图控制器的cellForRowAtIndexPath:方法而不是单元格中管理单元格的所有属性?这不是一个很好的方法,因为只有单元格才应该知道如何处理其自身的属性(给单元格编号,然后由单元格决定如何处理它-显示徽章,播放声音或也许会喊“二十四!”)。例如,如果将来您需要在另一个视图控制器中使用相同的单元格,则无需重写所有代码-这是您使用方法所要做的。

无论如何,显然您并没有在其prepareForReuse方法中重置单元格的徽章值。

尝试以这种方式实现(在单元格的.m文件中):

-(void)prepareForReuse {
    [super prepareForReuse];
    self.badge.badgeValue = 0;
}