gpt4 book ai didi

ios - Swift,无法用更多行更新 TableView

转载 作者:行者123 更新时间:2023-11-28 16:17:54 25 4
gpt4 key购买 nike

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

问题:当我运行更新 TableView 时出现错误

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).'

更新 tableview 数据源函数对我不起作用,这是代码:

到达底部时检测滚动

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

最佳答案

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

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

就是这样。 :)

创建了一个虚拟项目来回答。当您运行项目并滚动时,您会看到行是动态添加的。 https://github.com/harsh62/stackoverflow_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()
}

关于ios - Swift,无法用更多行更新 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38885417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com