问题
我使用多个UICollectionViews,每个UICollectionViews占用UITableView中的一个单元格,如下图所示:
每个表视图单元格都包含一个CollectionView,它通过dequeueReusableCellWithReuseIdentifier
创建具有相同nib标识符PlayerCell
的单元格。
问题是当用户向下滚动时,传入的Collection View单元格会调用collectionView:cellForItemAtIndexPath
来填充单元格。它重用了已消失的最顶层Collection View中的单元格,并且假设每个Collection View中最左侧面的索引路径相同(' 0'),它不会刷新Collection View中的面细胞
如何强制细胞刷新脸部,我应该在哪里进行此调用?
CODE EXTRACTS:
#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;
}
答案 0 :(得分:2)
问题不是UICollectionView
从前一个单元格获取单元格。问题是UITableView
需要UITableViewCell
从屏幕上退出以供重复使用,它包含您的所有UICollectionViewCells
。
尝试在reloadData
UICollectionView
添加updateCellWithPlayers: