2016-11-08 73 views
0

我必須在表格視圖單元格上播放視頻,但不希望重新使用視頻單元格,因爲它會再次重新創建視頻。停止重複使用視圖單元格的視圖

我試圖緩存數組中的單元格和從數組中加載(如果存在的話),但是當視圖彈出到前一個視圖時會造成一些問題。

即使視頻被刪除,視頻仍在播放。

AVTableViewCell *cachedCell = [self.cellList objectForKey:indexPath]; 
if (cachedCell) { 
    return cachedCell; 
} else { 
    AVTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AVTableViewCell" forIndexPath:indexPath]; 
    [cell configCell:indexPath]; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (section == 1) { 
    [self.cellList setObject:(AVTableViewCell *)cell forKey:indexPath]; 
    } 
} 

下面的代碼我在刪除視頻之前彈出視圖控制器,但仍然視頻或音頻播放某個時間。

- (IBAction)btnBackTapped:(id)sender { 

    // To Avoid the re-using video cell, implemented cache so make sure it got removed before going back to the previous screen. 

    AVTableViewCell *cachedCell = [self.cellList objectForKey:[NSIndexPath indexPathForRow:0 inSection:1]]; 
    if (cachedCell) 
    { 
     if (cachedCell.isJWVideo) { 
      [cachedCell.player stop]; 
      [cachedCell.player.view removeFromSuperview]; 
      cachedCell.player = nil; 
     } else { 
      [cachedCell.ytPlayerView stopVideo]; 
      [cachedCell.ytPlayerView clearVideo]; 
      cachedCell.ytPlayerView = nil; 
     } 

     [self.cellList removeAllObjects]; 
     self.cellList = nil; 
    } 

    [self.navigationController popViewControllerAnimated:YES]; 
} 

是否有任何其他技術在表格視圖單元格上播放視頻而不重複使用?因爲當我做上下滾動時,它會阻止滾動視圖再次加載視頻。

回答

0

我會建議不要緩存表格單元 - 你可能會遇到各種各樣的問題。

AVTableViewCell您應該覆蓋prepareForReuse方法並在那裏設置self.ytPlayerView = nil

這樣,剛剛重新使用的表格單元格會交給您的tableview代碼爲空,因此它們無法嘗試重新加載其舊視頻播放器視圖。

+0

感謝您的回覆。我已經嘗試過那部分,但是發生了什麼,它是在重新使用單元格時再次重新創建視頻,這意味着我必須等待看到已重新加載的視頻,因爲它已被刪除並重新加載。 ' - (void)prepareForReuse {super prepareForReuse];如果(self.isJWVideo){self.jwPlayer stop]; self.jwPlayer = nil;其他{ } [self.ytPlayerView stopVideo]; self.ytPlayerView = nil; } }' 你能告訴我上面的代碼有什麼問題嗎? – Jay

+0

它看起來不錯,但我從這裏不能告訴。你有沒有看過使用'tableView:didEndDisplayingCell:forRowAtIndexPath:'停止視頻並刪除播放器?這是檢測何時從表格中移除細胞的官方方式。 – norders

相關問題