【问题标题】:Reusable UICollectionViewCell doesn't refreshing可重用的 UICollectionViewCell 不刷新
【发布时间】:2017-01-11 06:29:25
【问题描述】:

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

在我的 CollectionViewController 中:

- (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;
}

在我的自定义 collectionView 单元格中:

@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

【问题讨论】:

  • wt 是节和行的数据源吗?
  • 我正在从 plist 文件中获取数据

标签: ios objective-c uicollectionview uicollectionviewcell uicollectionreusableview


【解决方案1】:

也许问题在于imageNamed: 缓存了图像。尝试改用imageWithContentsOfFile:

【讨论】:

  • 感谢 junk_key,你救了我的命...:)
【解决方案2】:

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

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

    CustomCollectionViewCell *cell = (CustomCollectionViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCell" forIndexPath:indexPath];

    for (UIView *subview in [cell subviews]) {
        [subview removeFromSuperview];
    }

    ...
}

【讨论】:

  • 不,这不起作用,在这种情况下,所有按钮都是空白的,没有显示背景图像。
猜你喜欢
  • 2014-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多