我有一个表格视图,每个单元格都可以播放一条消息,类似于 iphone 的语音邮件表格视图,不同之处在于滑块和进度标签是单元格的一部分:(
来源:apcmag.com)
该表与 CoreData 相关联,其中每个托管对象都包含指向表视图单元格的音频文件的链接。
由于播放按钮、滑块和音频时间进度标签是每个单元格的一部分,因此每个 AudioCell(继承自 UITableViewCell 的自定义单元格)都包含一个 AVAudioPlayer 对象来响应用户操作(例如播放、暂停等)。 .)。
当我开始播放单元格中的音频时,我需要将该单元格保存在 memvar 中,以便单元格内的 AVAudioPlayer 对象可以继续播放音频并且标签和滑块会正确更新。这是我将音频播放单元“保存”到 memvar 的代码:#pragma mark - AudioCellDelegate
- (void)playbackChangedForAudioCell:(AudioCell *)audioCell
{
if (self.audioPlayingCell.isPlaying && ![self.audioPlayingCell isEqual:audioCell]) {
[self.audioPlayingCell stopPlayback];
}
if (audioCell.isPlaying) {
self.audioPlayingCell = audioCell;
} else{
self.audioPlayingCell = nil;
}
// Mark the message as played.
Message *message = (Message *)[[self fetchedResultsControllerForTableView:self.tableView] objectAtIndexPath:audioCell.cellIndexPath];
[self messageSeenByUser:message];
}
这是相关代码,如果它与当前正在播放音频的 NSIndexPath 匹配,则从 memvar 中“恢复”单元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *managedObject = [[self fetchedResultsControllerForTableView:tableView] objectAtIndexPath:indexPath];
UITableViewCell *cell = nil;
// Implement any new kind of message here.
if ([managedObject isKindOfClass:[VoicemailMessage class]])
{
if ([self.audioPlayingCell.cellIndexPath isEqual:indexPath])
cell = self.audioPlayingCell;
else {
AudioCell *audioCell = (AudioCell *)[self setupCellForVoicemail:(VoicemailMessage*)managedObject];
audioCell.cellIndexPath = indexPath;
cell = audioCell;
}
}
问题是当 NSManagedObject 在以下位置更新时:
- (void)messageSeenByUser:(Message *)message
{
if (self.view.window) {
if ([message.isNew boolValue])
{
message.isNew = [NSNumber numberWithBool:NO];
[[NSNotificationCenter defaultCenter] postNotificationName:MessageBadgeNotification object:self];
}
}
}
单元格从表格视图中消失(空间仍然存在,但我看不到单元格中的任何小部件)。
但是,如果 NSManagedObject 没有更改并且表重新加载,则单元格不会消失。
我想知道是什么导致单元格消失,某些东西必须不同于常规的表格视图更新和托管对象的更改。