Swift,无法用更多行更新 table 视图

Swift, unable to update table view with more rows

目标: 我有一个从服务器获取的项目列表,显示在 TableView 中,项目分为 20 秒。首先,我加载了 20 个项目,然后当滚动到底部时我需要添加接下来的 20 个项目。

问题:当我运行更新table视图时出现错误

The number of rows contained in an existing section after the update (40) must be equal to the number of rows contained in that section before the update (20), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

更新table查看数据源函数对我不起作用,这里是代码:

到达底部时检测滚动

func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

    let offset = scrollView.contentOffset
    let bounds = scrollView.bounds
    let size = scrollView.contentSize
    let inset = scrollView.contentInset
    let y = CGFloat(offset.y + bounds.size.height - inset.bottom)
    let h = CGFloat(size.height)

    let reload_distance = CGFloat(-100)

      if (y > (h + reload_distance)) {

            pageNum += 1
            manager.getNewItems(String(pageNum)) //call the JSON
      }
}

TableView更新代码

//json response function    
func didReceiveResponse(info: [String : AnyObject]){

    // get the data and set in arrays (e.g. names[])
    // Then I reload my table and show first 20 Items MyTable.reload()
    // Working fine until here

   if pageNum > 0{

     //names.count is 40 now

     self.MyTable.beginUpdates()

                                             //I tried names.count-20 here
     tableView(MyTable, numberOfRowsInSection: (names.count))
               MyTable.insertRowsAtIndexPaths([
               NSIndexPath(forRow: (names.count)-20, inSection: 0)
               ], withRowAnimation: .Automatic)


            MyTable.endUpdates()
     }
}  

当然加载前 20 个是完美的工作,滚动完美调用函数,一切正常只是更新失败。

我为这个谜题困惑了 4 天,在网上找不到任何可行的解决方案。伙计们真的需要你们的帮助:)

谢谢

编辑:

这是我在 tableView

中填写数据的方式
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let MyReturnCell = MyTable.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! CustomCell

    MyReturnCell.name!.text = names[indexPath.row]
    //and so on until I fill all the data inside the MyCell

           return MyReturnCell

}

当您调用 insertRowsAtIndexPaths 时,数据源中存在的行数必须等于先前的计数加上要插入的行数。但是您似乎在 table 中插入了一个 NSIndexPath,但您的 names 数组大概还有 20 个项目。因此,请确保传递给 insertRowsAtIndexPaths 的数组中 NSIndexPath 项的数量等于已添加到 names.

的行数

以下代码示例可以帮助您了解您需要做什么:

  1. 创建索引路径数组
  2. 在您的数据源中添加您从服务器接收到的 20 个数据对象。
  3. 现在,由于您的数据源和索引路径数组位于同一页上,开始 table 查看更新并插入行。

就是这样。 :)

创建了一个虚拟项目来回答。当您 运行 项目并滚动时,您会看到行是动态添加的。 https://github.com/harsh62/Whosebug_insert_dynamic_rows

func insertRows() {
    let newData = ["Steve", "Bill", "Linus", "Bret"]
    let olderCount: Int = self.names.count

    names += newData

    var indexPaths = [NSIndexPath]()
    for i in 0..<newData.count {//hardcoded 20, this should be the number of new results received
        indexPaths.append(NSIndexPath(forRow: olderCount + i, inSection: 0))
    }
    // tell the table view to update (at all of the inserted index paths)
    self.tableView.beginUpdates()
    self.tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Top)
    self.tableView.endUpdates()
}