AVPlayerLayer в повторном использовании UITableViewCell

У меня есть логика воспроизведения видео в клетках.

Каждый раз, когда вы играете в ячейке и быстро перемещаете свиток, игрок снова появляется. (так как 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;
}

Несмотря на все попытки, он нестабилен (иногда работает, иногда нет), и по-прежнему возникает проблема с отображением слоя в ячейке, которая не принадлежит.

Кто-нибудь знает способ решить и / или контролировать это?

Спасибо за ваше время.

РЕДАКТИРОВАНИЕ

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;

0 ответов

Другие вопросы по тегам