2016-04-24 9 views
0

私のコードには、スクロールテーブルで特定の行にデータを読み込む際にいくつかの問題がありますが、その理由を見つけることができませんでした。スクロールしてデータをロードします。迅速。 UITableView

基本的には、要素をスクロールするときに次の10要素を読み込みたいと思っています。たとえば、テーブルに最初に10要素を読み込みたいとします。次に、5番目の要素にヒットすると、11〜20個の要素が読み込まれます。

私の問題は、テーブルを最初に読み込むときに、最初の5つの要素だけを読み込みますが、6-10の間にスペースがあることがわかります。次に、5番目の要素にテーブルをスクロールし、 (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

個人的に私はあなたが5わずか下からだ時に何をする探していることは、負荷10個の細胞..です少しこのif文を変更したい..しかし、それは簡単に読み込むことができますように、それは少し危険です複数回。あなたは、これまで行ってきた最高の成功の負荷を示し、

if indexPath.row == currentResultsTitleArray.count - 6 && indexPath.row > self.highestIndexFetchedOnをチェックするために、外部varが必要

がトリガーする必要があり、その後、成功した上で、フェッチ直前にデータを再ロード、新しいindexPath.rowself.highestIndexFetchedOnを設定

それは何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() 
} 

私はどちらか...私はすべての値を持っている辞書の1つの配列が良いだろうと思う、すべてのこれらの配列が異なるデータを収容して、ここでのデータ構造についてはよく分からないし、私たちがしているものを簡素化見ていますが、クラスの残りの部分を見ずに100%と言うことはできません。

関連する問題