且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在UITableViewCell中播放视频

更新时间:2023-01-25 19:15:33

永远不要在单元格中添加子视图:

Never add a subview to the cell:

[cell addSubview:moviePlayer.view];

仅将其添加到contentView:

Add it only to the contentView:

[cell.contentView addSubview:moviePlayer.view];

你所犯的另一个重大错误就是忘记了这个细胞可以重复使用;当用户滚动时,该单元格用于表格的不同行。因此它被重用,现在电影播放器​​视图仍在其中,即使现在这是表的错误行。您不仅需要将添加电影播放器​​视图添加到正确的单元格,还需要从所有错误的单元格中删除(在 else 部分代码。)

The other big mistake you're making is that you're forgetting that this cell can be reused; as the user scrolls, the cell be used for a different row of the table. So it gets reused, and now the movie player view is still in it even though this is now the wrong row of the table. You need not only to add the movie player view to the right cell, you need to remove it from all the wrong cells (in the else part of your code).