在自定义UITableViewCell中更改动画UIImage时。重新加载时(通常在滚动后),动画图像无法正确显示。
我尝试使用表格视图实现音频播放列表。我需要在自定义单元格中显示一个图标,指示单元格的音频是否正在播放。
1)这是视图层次结构:
------------------------------------
| custom UITableViewCell |
| ------------------------ |
| | UIImageView | |
| | --------------- | |
| | | UIImage | | |
| | --------------- | |
| ------------------------ |
------------------------------------
2)我有2张图片:
self.playIcon = [UIImage imageNamed:@"play"];
self.playingIcon = [UIImage animatedImageNamed:@"sentencePlayingNow" duration:0.5f];
3)当播放一个音频时,我在自定义UITableViewCell中设置了isPlaying属性。
// self.icon is a UIImageView
- (void)setPlaying:(BOOL)isPlaying
{
if (isPlaying) {
[self.icon setImage:self.playingIcon];
} else {
[self.icon setImage:self.playIcon];
}
_isPlaying = isPlaying;
}
4)我像这样重新加载tableView
// oldPlayingIndexPath and newPlayingIndexPath indicates current playing row.
if (oldPlayingIndexPath || newPlayingIndexPath) {
dispatch_async(dispatch_get_main_queue(), ^{
if (oldPlayingIndexPath) {
[self.tableView reloadRowsAtIndexPaths:@[oldPlayingIndexPath] withRowAnimation:UITableViewRowAnimationNone];
}
if (newPlayingIndexPath) {
[self.tableView reloadRowsAtIndexPaths:@[newPlayingIndexPath] withRowAnimation:UITableViewRowAnimationNone];
}
});
}
当我设置isPlaying属性时。如果self.playingIcon是静态UIImage,一切都很好。如果self.playingIcon像我上面描述的那样动画,那么它是self.icon是不显示还是显示self.playIcon(静态的)?那是为什么?
原始截图:
滚动后进入:
回复@RoryMcKinnel:我的cellForRowAtIndexPath代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"SentenceTableViewCell";
SentenceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[self configureCell:cell forRowAtIndexPath:indexPath];
return cell;
}
- (void)configureCell:(SentenceTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
Sentence *sentence = [self.fetchedResultsController objectAtIndexPath:indexPath];
[cell setTitle:sentence.text];
if ([self.article.allowsEditing boolValue] || (sentence.audio_url.length > 0)) {
[cell setCanPlay:YES];
} else {
[cell setCanPlay:NO];
}
cell.playIcon = self.playIcon;
cell.playingIcon = self.playingIcon;
cell.OKIcon = self.OKIcon;
cell.isPlaying = self.currentPlayingIndexPath && [self.currentPlayingIndexPath compare:indexPath] == NSOrderedSame;
__weak EditViewController *weakSelf = self;
cell.play = ^void() {
[weakSelf playSentenceAtIndexPath:indexPath];
};
cell.stop = ^void() {
[weakSelf.player pause];
weakSelf.currentPlayingIndexPath = nil;
};
cell.edit = ^void() {
weakSelf.tableView.editing = YES;
};
}
答案 0 :(得分:0)
这很可能是由于可重用性。您需要确保在重用或重新创建单元格时重新加载正确的图像。一个提示,您可能不希望错误的图标在出现正确的图标之前短暂闪烁,因此我会在didEndDisplayingCell
中将imageView设置为nil,并始终在viewForRowAtIndexPath