如何强制刷新重用的 collectionview 单元格?

How do I force a refresh of reused collectionview cells?

问题

我使用多个 UICollectionView,每个 CollectionView 在 UITableView 中占据一个单元格,如下图所示:

每个 Table View Cell 包含一个 CollectionView,它通过 dequeueReusableCellWithReuseIdentifier 创建具有相同笔尖标识符 PlayerCell 的单元格。

问题是当用户向下滚动时,传入的集合视图单元格调用 collectionView:cellForItemAtIndexPath 来填充单元格。它重用了最顶层的 Collection View 中已经消失的单元格,并且假设每个 Collection View 中最左边的人脸的索引路径相同('0'),它不会刷新 Collection View 单元格中的人脸。

如何强制单元格刷新面部,我应该在哪里调用?

代码摘录:

#ScoreCheckViewController.m
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [scores count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RankingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rankingCell"];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RankingCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    [cell updateCellWithPlayers:[self getPlayersInfo:[[[scores objectAtIndex:indexPath.section] allValues] firstObject]]score:[[[scores objectAtIndex:indexPath.section] allKeys] firstObject] place:indexPath.section+1];
    return cell;
}

#RankingCell.m
-(void)updateCellWithPlayers:(NSArray *)players score:(NSString *)score place:(NSInteger)place{
    self.players = players;
    self.score = score;
    scoreLabel.text = [NSString stringWithFormat:@"%@ pts.",score];
    placeLabel.text = [self placeFormat:place];
    placeLabel.textColor = [self placeColor:place];
    bracketImageView.image = [self placeImage:place];

    rankingCollectionView.frame = CGRectMake(rankingCollectionView.frame.origin.x, rankingCollectionView.frame.origin.y, rankingCollectionView.frame.size.width, [self rowHeightForArray:self.players]);
    bracketImageView.frame = CGRectMake(bracketImageView.frame.origin.x, bracketImageView.frame.origin.y, bracketImageView.frame.size.width, rankingCollectionView.frame.size.height);
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, rankingCollectionView.frame.origin.y + rankingCollectionView.frame.size.height+10);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    [rankingCollectionView registerNib:[UINib nibWithNibName:@"PlayerCell" bundle:nil] forCellWithReuseIdentifier:@"PlayerCell"];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PlayerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlayerCell" forIndexPath:indexPath];
    UserObject *userObj = [self.players objectAtIndex:indexPath.section];
    [cell updateCellWithPlayerImage:userObj.image name:userObj.shortName];

    return cell;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return [self.players count];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 1;
}

问题不在于 UICollectionView 从上一个单元格中获取单元格。问题是 UITableView 需要 UITableViewCell 从屏幕上出来以供重复使用,它包含你所有的 UICollectionViewCells.

尝试在 updateCellWithPlayers:

中为您的 UICollectionView 添加 reloadData