I have a logic of playing videos in the cells.
Each time you do play in a cell and move the scroll quickly, the player reappears again. (because dequeueReusableCellWithIdentifier
).
It occurred to me save the section and row of the cell and ask why whenever cellForRowAtIndexPath
is activated to show the layer. -> layer.hidden = NO;
Also in a poor attempt without positive results, the hidden layer disappears when the cell by doing the following:
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == playInRow && indexPath.section == playInSection) {
layer.hidden = YES;
}
}
I even try to support me in this other function:
- (BOOL)isPlayingRowVisible {
NSArray *indexes = [tableClips indexPathsForVisibleRows];
for (NSIndexPath *index in indexes) {
if (index.row == playInRow && index.section == playInSection) {
return YES;
}
}
return NO;
}
Although all attempts, it is not stable (sometimes works, sometimes not) and the problem still occurs to show the layer in a cell that does not belong.
Anyone know a way to solve and / or control this?
Thanks for your time.
EDITED
cellForRowAtIndexPath
code.
- (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;
}
EDITED 2
Add layer to cell
When the user presses a button that is inside the cell
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;