菜鸟教程小白 发表于 2022-12-11 19:31:52

ios - 如何防止单元格的 avplayer - Tableview 重新初始化


                                            <p><p>我陷入了一个大问题。
我有一个带有视频播放器的 tableview - AVPlayer
我有 10 个单元格,每个单元格都在播放视频。但问题是当我
scroll tableview 播放器重新初始化并从中播放视频
开始。我想阻止这种重新初始化并播放像推特这样的视频
我们离开的最后一场比赛应该尝试什么代码?我尝试了多个代码但没有成功</p>

<pre><code>    Code is here

    if (Arr_avPlayer.count &gt; indexPath.row){
      Cell_TVC * cell = (Cell_TVC *)[Arr_avPlayer
      objectAtIndex:indexPath.row];
      return cell;
    }

   //    Cell_TVC *cell = (Cell_TVC *)[tableView
      dequeueReusableCellWithIdentifier:@&#34;Cell_TVC&#34;
      forIndexPath:indexPath];

         **Cell_TVC *cell = [ init];**

         //    Cell_TVC *cell =[tableView

               dequeueReusableCellWithIdentifier:@&#34;Cell_TVC&#34;];
          //    if (!cell) {
          //      cell = [[Cell_TVC

               alloc]initWithStyle:UITableViewCellStyleDefault
                         reuseIdentifier:@&#34;Cell_TVC&#34;];
         //            
         //    }



      AVURLAsset *asset = [ initWithURL:[NSURL
      fileURLWithPath:[self filepath:[myIntegers
      objectAtIndex:indexPath.row]]] options:nil];

      cell.avPlayerItem = [ initWithAsset:asset];
      cell.avPlayer = [
      initWithPlayerItem:cell.avPlayerItem];

    cell.avPlayerLayer =[ init];
    cell.avPlayerLayer = [AVPlayerLayer
    playerLayerWithPlayer:cell.avPlayer];
    cell.lblTime.text = @&#34;0.0&#34;;

    CGRect contentViewFrame = cell.contentView.frame;
    contentViewFrame.origin.x = 0;
    contentViewFrame.origin.y = 25;
    contentViewFrame.size.width = APP_width;
    contentViewFrame.size.height = APP_width * 0.55;
    cell.contentView.frame = contentViewFrame;
    cell.contentView.backgroundColor = ;

    UILabel *lbl = [[UILabel
   alloc]initWithFrame:CGRectMake((cell.contentView.frame.size.width / 2
    ) - 100, contentViewFrame.size.height + 10, 200, 100)];

    //lbl.text = [NSString stringWithFormat:@&#34;%02f:%02f&#34;, dMinutes11,
               dSeconds11];

    lbl.tag = indexPath.row;

    ;
    ;

    ;
    ;


    if i write the cell like below

    Cell_TVC *cell = [ init];

   i am not able to acess the property of cell and when i use like
    below

   if (!cell)
   {
               cell = [[Cell_TVC
               alloc]initWithStyle:UITableViewCellStyleDefault
               reuseIdentifier:@&#34;Cell_TVC&#34;];

   }
</code></pre>

<p>播放器索引更改和视频获取更改其索引
<strong>如何解决这个问题?</strong></p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果您使用的是 UItableview,它的行为很明显。 UItableview 不会将所有单元格保存在内存中。随着单元格从屏幕上消失,tableview 将其删除并在您向下/向上滚动时再次添加(当新单元格出现在屏幕上时)。
您无法更改 UItableview 的这种行为。</p>

<p>有一个丑陋的解决方案可用:将您的表格 View 调整为 <code>height = HeightOfCell * numberOfCell.</code> 但不建议这样做。因为这可能会造成内存问题。</p>

<p>更好的解决方案是跟踪 Seek 时间。您可以使用 <a href="https://developer.apple.com/reference/uikit/uitableviewdelegate/1614870-tableview?language=objc" rel="noreferrer noopener nofollow">didEndDisplayingCell</a>将 Player 的当前寻道时间的值存储在将删除的单元格中。当该索引路径的单元格再次出现时(cellforrow),您可以将播放器搜索到存储的值。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何防止单元格的 avplayer - Tableview 重新初始化,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/43518825/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/43518825/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何防止单元格的 avplayer - Tableview 重新初始化