簡體   English   中英

在UITableView中快速創建分頁

[英]Swift create pagination in UITableView

我有UITableView與顯示來自數組的緩存數據

我想像這樣的結構在UITableView分頁:

1)當我使用UITableView進行查看時-它向我顯示了所有緩存數據(這是我所做的,並且可以正常工作)

2)當用戶向下滾動並且if caching data left 5調用函數將新的10個數據加載到數組中,並顯示在UITableView並添加recheck(當表行顯示20行中的15個時,調用函數加載更多數據並添加)

例如:在緩存數組中,我有10個數據,函數load new,在表中顯示20行數據

我嘗試這樣做:

public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        print(indexPath.row)

        let lastItem = array.count - 1
        if indexPath.row == lastItem {
            loadMoreData()
        }
    }

 func loadMoreData {
        for _ in 0...9 {
            //load data from server
        }
       tableView.reloadData()
    }

取一個布爾變量來檢查是否正在從api加載數據。

var isLoading = false

添加以下代碼:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if !isLoading && indexPath.row == array.count - 5 {
        isLoading = true
        loadMoreData()
    }
}

一旦從api獲取數據,就設置isLoading = false

希望這個能對您有所幫助 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM