【问题标题】:How do I force a refresh of reused collectionview cells?如何强制刷新重用的 collectionview 单元格?
【发布时间】:2015-09-08 04:42:30
【问题描述】:

问题

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

每个 Table View Cell 都包含一个 CollectionView,它通过 dequeueReusableCellWithReuseIdentifier 创建具有相同 nib 标识符的单元格 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;
}

【问题讨论】:

    标签: ios iphone cocoa-touch uicollectionview reuseidentifier


    【解决方案1】:

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

    尝试在updateCellWithPlayers: 中为您的UICollectionView 添加reloadData

    【讨论】:

    • 小时。我花了几个小时在这...谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 2020-04-19
    • 1970-01-01
    相关资源
    最近更新 更多