0

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;
jose920405
  • 7,982
  • 6
  • 45
  • 71
  • This is because UITableView caches tabiewView cells. Can you please show your cellForRowAtIndexPath code? – shpasta Jan 13 '16 at 17:11
  • You don't initialise the cell. After you have obtained the "cellV", you need to tell it what to do. Probably http://stackoverflow.com/questions/4620095/uitableview-and-cell-reuse?rq=1 will give you an answer how to do that. – fishinear Jan 13 '16 at 17:47
  • @jose920405 "layer" is a global variable? How do you define it? – shpasta Jan 13 '16 at 18:14
  • @shpasta layer is a global variable. I just need a layer for all cells. When this finishes playing is destroyed with `removeFromSuperview` – jose920405 Jan 13 '16 at 18:36
  • So you have one layer for multiple cells? How do you add layer on cells? – shpasta Jan 13 '16 at 22:27
  • @shpasta code for add layer on cells added. – jose920405 Jan 14 '16 at 13:09

0 Answers0