停止重复使用视图单元格的视图
问题描述:
我必须在表格视图单元格上播放视频,但不希望重新使用视频单元格,因为它会再次重新创建视频。停止重复使用视图单元格的视图
我试图缓存数组中的单元格和从数组中加载(如果存在的话),但是当视图弹出到前一个视图时会造成一些问题。
即使视频被删除,视频仍在播放。
AVTableViewCell *cachedCell = [self.cellList objectForKey:indexPath];
if (cachedCell) {
return cachedCell;
} else {
AVTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AVTableViewCell" forIndexPath:indexPath];
[cell configCell:indexPath];
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (section == 1) {
[self.cellList setObject:(AVTableViewCell *)cell forKey:indexPath];
}
}
下面的代码我在删除视频之前弹出视图控制器,但仍然视频或音频播放某个时间。
- (IBAction)btnBackTapped:(id)sender {
// To Avoid the re-using video cell, implemented cache so make sure it got removed before going back to the previous screen.
AVTableViewCell *cachedCell = [self.cellList objectForKey:[NSIndexPath indexPathForRow:0 inSection:1]];
if (cachedCell)
{
if (cachedCell.isJWVideo) {
[cachedCell.player stop];
[cachedCell.player.view removeFromSuperview];
cachedCell.player = nil;
} else {
[cachedCell.ytPlayerView stopVideo];
[cachedCell.ytPlayerView clearVideo];
cachedCell.ytPlayerView = nil;
}
[self.cellList removeAllObjects];
self.cellList = nil;
}
[self.navigationController popViewControllerAnimated:YES];
}
是否有任何其他技术在表格视图单元格上播放视频而不重复使用?因为当我做上下滚动时,它会阻止滚动视图再次加载视频。
答
我会建议不要缓存表格单元 - 你可能会遇到各种各样的问题。
在AVTableViewCell
您应该覆盖prepareForReuse
方法并在那里设置self.ytPlayerView = nil
。
这样,刚刚重新使用的表格单元格会交给您的tableview代码为空,因此它们无法尝试重新加载其旧视频播放器视图。
感谢您的回复。我已经尝试过那部分,但是发生了什么,它是在重新使用单元格时再次重新创建视频,这意味着我必须等待看到已重新加载的视频,因为它已被删除并重新加载。 ' - (void)prepareForReuse {super prepareForReuse];如果(self.isJWVideo){self.jwPlayer stop]; self.jwPlayer = nil;其他{ } [self.ytPlayerView stopVideo]; self.ytPlayerView = nil; } }' 你能告诉我上面的代码有什么问题吗? – Jay
它看起来不错,但我从这里不能告诉。你有没有看过使用'tableView:didEndDisplayingCell:forRowAtIndexPath:'停止视频并删除播放器?这是检测何时从表格中移除细胞的官方方式。 – norders