可重用 UICollectionViewCell 不刷新



我的UICollectionViewCell在情节提要中有一个按钮,并且从文档目录动态设置按钮数量的背景图像,从而产生诸如某些按钮背景图像不刷新等问题,即使我关闭控制器并再次加载该控制器,它们仍保留旧图像,问题仍然存在。如何解决这个问题?

在我的收藏视图控制器中:

- (NSString *)getBackgroundImage:(NSIndexPath *)indexPath {
    NSArray *indexes = [self.allColors[indexPath.section] objectForKey:@"indexes"];
    return [NSString stringWithFormat:@"%@/%@/%@/combimeter_button/combimeter_%ld_%@@2x.png", [self.documentDirectory getDocumentsDirectory], _selectedCar, _selectedCar, (long)[indexes[indexPath.row] integerValue], [self getColorKey:indexPath.section]];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CustomCollectionViewCell *cell = (CustomCollectionViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCell" forIndexPath:indexPath];
    cell.layer.borderWidth = 2.0f;
    cell.layer.borderColor = [[self getBackgroundColor:indexPath.section] CGColor];
    [cell.cellButton setBackgroundImage:[UIImage imageNamed:[self getBackgroundImage:indexPath]] forState:UIControlStateNormal];
    [cell.cellButton addTarget:self action:@selector(combimeterButtonDidSelected:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

在我的自定义集合中查看单元格:

@interface CustomCollectionViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIButton *cellButton;
@end
@implementation CustomCollectionViewCell
-(void)prepareForReuse {
    NSLog(@"Is it possible to clear here ? but this method didn't call in my code");
}
@end

也许问题是imageNamed:缓存图像。请尝试改用imageWithContentsOfFile:

在创建新视图之前,请尝试删除所有以前的视图:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CustomCollectionViewCell *cell = (CustomCollectionViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCell" forIndexPath:indexPath];
    for (UIView *subview in [cell subviews]) {
        [subview removeFromSuperview];
    }
    ...
}

相关内容

  • 没有找到相关文章

最新更新