列表的一半会在开头加载,当到达列表末尾时,另一半会添加到 table 视图下方

Half of the list will be loaded at the beginning, and when it comes to the end of this list, the other half will be added below inside table view

我有一个 tableView 并且 json data.I 想要 tableview 在 json 数据中加载一半。当 table 视图滚动到上半场结束时,另一半将是 load.Can 谁知道我该怎么做?

我相信您将不得不在 scrollViewDidScroll 的帮助下在您这边进行一些 pagination 类型的处理,以观察何时到达 tableview 的末尾以加载下一批

首先设置一些初始变量来帮助您跟踪分页过程。

var pageStart = 0
var pageSize = 15

// scrollViewDidScroll will be called several times
// so we need to be able to block the loading process
var isLoadingItems = false

// Array of 50 values which you get from the API
// For example your JSON data
var totalNumberOfItems = (0...49).reduce([Int]())
{ (result, number) in
    
    var array = result
    array.append(number)
    return array
}

// This will periodically be filled with items from totalNumberOfItems
var itemsToLoad: [Int] = []

我创建了这个加载函数,它有助于批量添加项目并适当地重新加载 tableview

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    loadItems()
}

private func loadItems(withDelay delay: Double = 0) {
    
    // Check that there are items to load
    if pageStart < totalNumberOfItems.count, !isLoadingItems {
        
        isLoadingItems = true
        
        // The delay and dispatch queue is just to show you the
        // loading of data in batches, it is not needed for the
        // solution
        if delay > 0 {
            presentLoader()
        }
        
        DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
            
            if delay > 0 {
                
                // dismiss the loader
                self.dismiss(animated: true, completion: nil)
            }
            
            // Calculate the page end based on the page size or on
            // the number of items remaining to load if the page size
            // is greater than the number of items remaining
            var pageEnd = self.pageStart + self.pageSize - 1
            
            if pageEnd > self.totalNumberOfItems.count {
                let remainingItems = self.totalNumberOfItems.count - self.pageStart - 1
                pageEnd = self.pageStart + remainingItems
            }
            
            let newItems = Array(self.totalNumberOfItems[self.pageStart ... pageEnd])
            self.itemsToLoad.append(contentsOf: newItems)
            self.pageStart = pageEnd + 1
            
            let indexPaths = newItems.map { IndexPath(row: [=11=],
                                                      section: 0) }
            
            // Update the table view
            self.tableView.beginUpdates()
            self.tableView.insertRows(at: indexPaths, with: .fade)
            self.tableView.endUpdates()
            
            // scroll to first newly inserted row
            if let firstNewRow = indexPaths.first {
                self.tableView.scrollToRow(at: firstNewRow,
                                           at: .top,
                                           animated: true)
            }
            
            
            // Unlock the loading process
            self.isLoadingItems = false
        }
    }
}

然后观察是否使用 scrollViewDidScroll 到达结尾并重新加载 tableview 以显示插入的行

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
    // Check that the current scroll offset is > 0 so you don't get
    // any false positives when the table view is set up
    // and check if the current offset has reached the end
    if scrollView.contentOffset.y >= 0 &&
        scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height) {
        
        print("reached end, load more")
        
        // Load more data
        loadItems(withDelay: 1)
    }
}

外观如下:

如果您发现某些部分难以遵循或添加到您自己的代码中,可以找到 运行 原样的完整代码 here: