问题
我使用了多个 UICollectionViews,每个 UICollectionViews 在 UITableView 中占据一个单元格,如下图所示:
dequeueReusableCellWithReuseIdentifier
每个 Table View Cell 都包含一个 CollectionView,它使用相同的 nib 标识符创建单元格PlayerCell
。
问题是当用户向下滚动时,传入的集合视图单元格调collectionView:cellForItemAtIndexPath
用以填充单元格。它重用了已消失的最顶层集合视图中的单元格,并且鉴于每个集合视图中最左侧面的索引路径相同(“0”),它不会刷新集合视图单元格中的面。
如何强制单元格刷新面部,我应该在哪里拨打这个电话?
代码摘录:
#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;
}