2016-04-24 99 views
0

我的代碼有一些問題通過滾動表將數據加載到某一行,但我找不到原因。滾動以加載數據。迅速。 UITableView

基本上,我希望表格在滾動到總元素時加載下10個元素 - 例如,我希望表格最初加載10個元素。然後,當它擊中第五個元素時,它加載11-20個元素。

我的問題是,當我最初加載表時,它只加載前5個元素,但我可以看到6-10之間有一些空格,然後我滾動表格到第五個元素,它加載下10個元素(6-15)。

let rowPerPage = 10 

func refreshResults(){ 
    // append data from server // 
    self.resultsTitleArray.append(...) 

    if self.resultsTitleArray.count != 0 { 
     let minCount = min(rowPerPage, self.productImageArray.count) - 1 
     self.currentResultsTitleArray += self.resultsTitleArray[0...minCount] 
    } 

    self.resultsTable.reloadData() 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ 
    return currentResultsTitleArray.count 
} 

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    let diff = rowPerPage - 5 
    let currentDiff = currentResultsTitleArray.count - 1 - indexPath.row 
    if currentResultsTitleArray.count != resultsTitleArray.count && diff == currentDiff { 
     let minCount = min(resultsTitleArray.count, currentResultsTitleArray.count + rowPerPage as Int) - 1 
     let next = self.currentResultsTitleArray.count 
     self.currentResultsTitleArray += self.resultsTitleArray[next...minCount] 
     resultsTable.reloadData() 
    } 
} 

回答

0

個人而言,我會稍微改變這種if聲明。什麼你希望做的是負載10個細胞,當你5遠離只有底部..但它是稍微危險的,因爲它可以很容易地加載多次。

您需要一個外部var顯示到目前爲止你做過的最成功的負載,並檢查

if indexPath.row == currentResultsTitleArray.count - 6 && indexPath.row > self.highestIndexFetchedOn應該是動了扳機,然後在成功提取,只需重新加載數據之前,設置self.highestIndexFetchedOnindexPath.row

目前還不清楚是什麼rowPerPage做,但我猜這是10,也是minCount邏輯看起來正確,但我會打印出來,以確保你得到你所期望的。

if indexPath.row == currentResultsTitleArray.count - 4 && indexPath.row > self.highestIndexFetchedOn { 

    let minCount = min(resultsTitleArray.count, currentResultsTitleArray.count + rowPerPage as Int) - 1 
    let next = self.currentResultsTitleArray.count 

    print("Count:",minCount) 

    self.currentResultsTitleArray += self.resultsTitleArray[next...minCount] 

    // etc... 

    self.highestIndexFetchedOn = indexPath.row 
    tableView.reloadData() 
} 

我不知道有關數據結構在這裏與所有這些陣列容納不同的數據要麼...我認爲詞典的一個數組,在所有的值會更好,簡化了我們是看着..但我不能說沒有看到其他的班級100%..