我有以下情况:
具体来说,我们有这样的代码:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = (MyCell *) [collectionView dequeueReusableCellWithReuseIdentifier: @"MyCellType" forIndexPath: indexPath];
NSString *str = [NSString stringWithFormat:@"%d", [indexPath row]];
[cell.answerButton setTitle: str forState:UIControlStateNormal];
// Set tag on button to mark which row no
// Set button background image depending on some internal data
return cell;
}
- (IBAction)answerButtonPressed:(id)sender {
UIButton *button = (UIButton *) sender;
// look at button tag, decide which button pressed, update internal
// data which might mean we need to change button colours
[_collectionView reloadData];
}
我尝试调用reloadData:在dispatch_async中“延迟”更新(我来自Java背景,并且在SwingUtilities.invokeLater()中放置UI更新有时可以帮助这样的情况),但是这没有什么区别。
任何人都可以提供帮助:我做错了什么,或者这只是我必须忍受的UI故障?
答案 0 :(得分:1)
而不是为整个collectionView重新加载数据,尝试精确定位需要更新的单元格。
- (IBAction)answerButtonPressed:(id)sender {
UIButton *button = (UIButton *) sender;
// look at button tag, decide which button pressed, update internal
// data which might mean we need to change button colours
NSInteger thisTag = button.tag;
NSIndexPath* thisIndexPath = [NSIndexPath indexPathForItem:thisTag inSection:0];
[_collectionView reloadItemsAtIndexPath:@[thisIndexPath]];
}