-1

我正在开发一个 tableView,其中每个单元格由 AVPlayer 组成,并且高度为 view.frame.size.height 并且还启用了分页。所以本质上它的工作方式类似于 tiktok 应用程序提要。我遇到的问题是我必须在 prepareForReuse 中销毁 AVPlayer 以在调用 cellForRow 时显示新视频,否则如果我不销毁它并且用户快速滚动,则较旧的视频会出现一秒钟,如果我销毁每个播放器使用之前的时间,然后 AVPlayer 需要一秒钟来加载和显示黑屏之间。它有效,但结果并不优雅。

那么有什么方法可以预加载单元格并将它们保存在数组中。就像一个由三个独立单元组成的数组。我们可以在用户滚动时更改其中的值

例如

[] represents cell on screen

0 [1] 2

array[1] would always be the cell on screen 
array[0] previous
array[2] next

if user scroll down then
array[0] = array[1]
array[1] = array[2] 
array[2] = create next one (proactively)

if user scroll up then
let array1 = array[1]
array[1] = array[0]
array[0] = array1
array[2] = create new one
4

2 回答 2

0

这听起来更像是收集视图的工作。一旦收到数据,您应该能够通过预取和填充单元格以实现所需的平滑加载。

https://developer.apple.com/documentation/uikit/uicollectionviewdatasourceprefetching/prefetching_collection_view_data

预取集合视图数据

在显示之前加载集合视图单元格的数据。

集合视图以可自定义的布局显示有序的单元格集合。UICollectionViewDataSourcePrefetching 协议通过预取即将到来的集合视图单元格所需的数据来帮助提供更流畅的用户体验。当您启用预取时,集合视图会在需要显示单元格之前请求数据。当需要显示单元格时,数据已经在本地缓存。

下图显示了已预取的集合视图边界之外的单元格。

如果这不能满足您的要求,请发布您的代码。

于 2020-05-13T05:36:20.573 回答
0

更新:

我花了很多时间试图用 tableView 解决这个问题,但无法这样做。我最终制作了一个带有滚动视图的自定义表格视图,它在问题中提出了类似的实现。所以使用滚动视图来预加载单元格是可行的。下面提供的代码仅供参考

  func scrollViewDidScroll(_ scrollView: UIScrollView) {
    //Condition for first cell
    if currentIndex == 0 && (scrollView.contentOffset.y > view.frame.minY) {
        //pageIndex will only be one if the user completly scrolls the page, if he scroll half a returns it will remain 0
        if pageIndex == 1 {
            //so if the user completely scroll to page 1 the change the previous index to 0
            //new current index will be 1
            //call scrollForward which will prepare the cell for index 2
            previousIndex = currentIndex
            currentIndex = Int(pageIndex)
            if shouldCallScrollMethods {
                scrollForward()
            }

            guard let currentCell = getCellOnScreen() else { return }
            currentCell.refreshBottomBarView()
        }
    //Condition for rest of the cells
    } else {
        //this condition checks if the user completly scroll to new page or just drag the scrollview a bit gets to old position
        if (pageIndex != currentIndex) {
            //Update the previous and current to new values
            previousIndex = currentIndex
            currentIndex = Int(pageIndex)
            //Checks if the user is scroll down the calls scrollForwad else scrollBackward
            if shouldCallScrollMethods {
                if currentIndex > previousIndex {
                   scrollForward()
                } else {
                   scrollBackward()
                }
            }
            guard let currentCell = getCellOnScreen() else { return }
            currentCell.refreshBottomBarView()
        }
    }
}

private func scrollForward() {
    //print("scrollForward")
    addCell(at: currentIndex + 1, url: getPlayerItem(index: currentIndex + 1))
    removeCell(at: currentIndex - 2)

}

private func scrollBackward() {
    //Condition to check if the element is not at 0
    //print("scrollBackward")
    addCell(at: currentIndex - 1, url: getPlayerItem(index: currentIndex - 1))
    removeCell(at: currentIndex + 2)
}
于 2020-06-05T15:07:07.817 回答