2017-02-15 46 views
0

我目前使用CloudKit作爲我的後端,到目前爲止,我一直在享受它。iOS - 用戶到達滾動視圖底部時加載項目

我有一個查詢發生,檢索數據填充表視圖。由於我不知道它可能有的項目數,我正在過濾查詢X個項目(比如說15)。

我的目標是,當用戶滾動到表格視圖底部(最後查詢的項目)生病查詢後端繼續填充表格視圖。

我已經搜索,但無法找到這樣做的CloudKit代碼。

有人可以給我一個提示如何做到這一點?

謝謝大家給予的幫助。 最好,伊萬。

+0

爲什麼要注意使用'uitableview'請求服務器的結果?你可以用鍵搜索並得到許多結果:「UITableView加載更多」 – anhtu

+0

我錯過了寫的問題我沒有使用滾動視圖我正在使用tableView –

回答

1

關於IOS我認爲你必須使用UITableViewController或者只是使用UITableView。

添加一個UIActivityIndi​​catorView(即微調)到你的UITableViewController。出口連接到代碼:

@IBOutlet weak var spinner: UIActivityIndicatorView! 

一個屬性添加到您的UITableViewController跟蹤過你現在正在加載更多數據,讓你不要試圖做兩次:

var loadingData = false 

啓動微調器動畫,然後調用refreshRes():

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
if !loadingData && indexPath.row == refreshPage - 1 { 
    spinner.startAnimating() 
    loadingData = true 
    refreshRes() 
} 

讓refreshRes()在後臺線程上運行。這將使您的桌子仍然可以自由移動。動畫微調將告訴用戶更多的數據即將到來。一旦您的查詢返回,更新主線程上的表數據。

func refreshRes() { 
dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)) { 
    // this runs on the background queue 
    // here the query starts to add new 15 rows of data to arrays 

    dispatch_async(dispatch_get_main_queue()) { 
     // this runs on the main queue 
     self.refreshPage += 15 
     self.tableView.reloadData() 
     self.spinner.stopAnimating() 
     self.loadingData = false 
    } 
} 

之後,它的依賴,你必須做出讓別人15個DATAS

+0

非常感謝。 –

+0

@G Clovs我想知道你如何使用refreshPage屬性?我需要一個特定的代表嗎? –

+0

我明白了。 refreshPage是檢索的當前項目跟蹤器 –

相關問題