2016-04-24 77 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%..