我有在单元格中播放视频的逻辑。
每次在单元格中进行游戏并快速移动滚动时,播放器会再次出现。 (因为dequeueReusableCellWithIdentifier
)。
我发现保存了单元格的部分和行,并询问为什么只要激活cellForRowAtIndexPath
来显示图层。 - > layer.hidden = NO;
同样在没有正面结果的不良尝试中,隐藏层在单元格中通过执行以下操作而消失:
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == playInRow && indexPath.section == playInSection) {
layer.hidden = YES;
}
}
我甚至试图在其他功能中支持我:
- (BOOL)isPlayingRowVisible {
NSArray *indexes = [tableClips indexPathsForVisibleRows];
for (NSIndexPath *index in indexes) {
if (index.row == playInRow && index.section == playInSection) {
return YES;
}
}
return NO;
}
虽然所有尝试都是不稳定的(有时是有效的,有时不是)但仍然会出现在不属于的单元格中显示图层的问题。
任何人都知道解决和/或控制这种方法的方法吗?
感谢您的时间。
EDITED
cellForRowAtIndexPath
代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SWTableViewCell * cellV;
ClipVo * clip = [myArray objectAtIndex:indexPath.row];
if ([clip.type isEqualToString:@"video"]) {
cellV = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cellVideoChat"];
if (cellV == nil) {
[[NSBundle mainBundle] loadNibNamed:@"ChatVideoTableViewCell" owner:self options:nil];
cellV = cellVideoChat;
UIView *view = [cellV viewWithTag:14];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickBtnChatVideo:)];
tapGesture.numberOfTapsRequired = 1;
[view addGestureRecognizer:tapGesture];
}
}
if (clip.isPlaying && playerClip.rate != 0.0f && [self isPlayingRowVisible] && indexPath.row == playInRow && indexPath.section == playInSection) {
layer.hidden = NO;
}
return cellV;
}
已编辑2
将图层添加到单元格
当用户按下单元格内的按钮时
playerClip = [[AVPlayer alloc] initWithURL:url];
layer = [AVPlayerLayer playerLayerWithPlayer:playerClip];
layer.backgroundColor = [UIColor blackColor].CGColor;
UIView *viewContainer = [cell viewWithTag:14];
layer.frame = CGRectMake(0, 0, viewContainer.frame.size.width, viewContainer.frame.size.height);
[layer setVideoGravity:AVLayerVideoGravityResizeAspect];
[viewContainer.layer insertSublayer:layer below:btnPlayVideo.layer];
[playerClip play];
playInRow = indexpath.row;
playInSection = indexpath.section;