Ios 在UITableView中销毁AVPlayer实例

Ios 在UITableView中销毁AVPlayer实例,ios,objective-c,uitableview,avplayer,pfquerytableviewcontrolle,Ios,Objective C,Uitableview,Avplayer,Pfquerytableviewcontrolle,我有一个UITableViewController,标题中有两个按钮 按钮1-从用户名API提取数据,刷新tableview,并为每个单元格显示一个字符串用户名 按钮2-从视频API中提取数据,刷新tableview,并在每个单元格中显示一个AVPlayer实例 当我按下视频按钮时,每个单元格都会很好地显示一个视频 当我按下用户名按钮时,我可以看到下面的用户名表视图,但上面是所有拒绝销毁的视频。我要想办法消灭他们 你知道如何立即销毁一个AVPlayer的多个实例吗 UITableViewCont

我有一个UITableViewController,标题中有两个按钮

按钮1-从用户名API提取数据,刷新tableview,并为每个单元格显示一个字符串用户名

按钮2-从视频API中提取数据,刷新tableview,并在每个单元格中显示一个AVPlayer实例

当我按下视频按钮时,每个单元格都会很好地显示一个视频

当我按下用户名按钮时,我可以看到下面的用户名表视图,但上面是所有拒绝销毁的视频。我要想办法消灭他们

你知道如何立即销毁一个AVPlayer的多个实例吗

UITableViewController:PFQueryTableViewController

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
        static NSString *CellIdentifier = @"Cell";

//the userOrVideoString is a string property assigned a value whenever the user presses the user or video button
        if([self.userOrVideoString isEqualToString:@"video"]) {
            VideoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[VideoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }
            NSString *dateString = [NSDateFormatter localizedStringFromDate:object.updatedAt
                                                                  dateStyle:NSDateFormatterShortStyle
                                                                  timeStyle:NSDateFormatterFullStyle];

            PFFile *file = [object objectForKey:@"videoFile"];
            Video *vid = [Video videoWithStringURL:file.url];

            //problem - easy to init these videos, can't delete them though? 
            [cell setVideo:vid];
            [cell play];
            cell.textLabel.text = dateString;
            return cell;
        }
        else {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }
            NSString *username = [object objectForKey:@"username"];
            cell.textLabel.text = username ;
            return cell;
        }

    }


-(void) didPressUserButton {
    self.userOrVideoString = @"User";
    [self.tableView reloadData];

    [self loadObjects];

}

-(void) didPressVideoButton {
    self.userOrVideoString = @"video";
    [self.tableView reloadData];
    [self loadObjects];

}
自定义VideoCell:UITableViewCell

- (void)setVideo:(Video *)video
{
    NSLog(@"%s : %@", __PRETTY_FUNCTION__, video);

   //videoPlayer is an AVPlayer as a property 
    [self.videoPlayer replaceCurrentItemWithPlayerItem:[AVPlayerItem playerItemWithAsset:video.video]];
    [self.videoPlayer setActionAtItemEnd:AVPlayerActionAtItemEndNone];
    self.videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];
    [self.videoLayer setPlayer:self.videoPlayer];
    [self.videoLayer setBackgroundColor:[[UIColor blackColor] CGColor]];
    [self.videoView.layer addSublayer:self.videoLayer];
    self.videoPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playerItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:[self.videoPlayer currentItem]];

    // Here we add an KVO to the player to know when the video is ready to play
    // This is important if you want the user to see something while the video is being loaded
    [self.videoPlayer addObserver:self forKeyPath:@"status" options:0 context:nil];

    // Here we need to add the indicator to the video views layer
    // Then start animating
    // But if the video is already ready to play, then we dont add it
    // This case comes up when the video has been cached like we have done so
    // and the cell is being reused
    if ([self.videoPlayer status] != AVPlayerStatusReadyToPlay) {
        [self.videoView.layer addSublayer:[self.indicator layer]];
        [self.indicator startAnimating];
    }

}

- (void)play
{
    [self.videoPlayer play];
}

有什么解决办法吗?