gpt4 book ai didi

swift - 使用 Swift 在 TableView 中插入部分

转载 作者:行者123 更新时间:2023-11-28 07:29:27 24 4
gpt4 key购买 nike

我正在尝试在 tableview 中插入一个新部分

let oldIns = insertCounter //insertCounter = 40
insertCounter += Int(INSERT_MESSAGES) // insertCounter = 80
let minn = min(insertCounter, Int(dbmessages.count))
print(minn) // minn = 80
tableView.beginUpdates()
tableView.insertSections(NSIndexSet(indexesIn: NSMakeRange(0, minn)) as IndexSet, with: .top)
tableView.endUpdates()


override func numberOfSections(in tableView: UITableView) -> Int {
let a = min(insertCounter, Int(dbmessages.count))
print(a) // after call insertSections a = 80
return min(insertCounter, Int(dbmessages.count))
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return 5
}

我得到一个错误:

Invalid update: invalid number of sections. The number of sections contained in the table view after the update (80) must be equal to the number of sections contained in the table view before the update (40), plus or minus the number of sections inserted or deleted (80 inserted, 0 deleted).

当我执行以下操作时:

tableView.insertSections(NSIndexSet(indexesIn: NSMakeRange(oldIns, minn)) as IndexSet, with: .top)

我得到错误:

exception 'NSInternalInconsistencyException', reason: 'attempt to insert section 80 but there are only 80 sections after the update'

我做错了什么?

最佳答案

如错误所述,您的旧节数为 40,您插入了 80 个节,因此更新后的节数应为 120。

override func numberOfSections(in tableView: UITableView) -> Int {
let a = min(insertCounter, Int(dbmessages.count))
print(a) // after call insertSections a = 80
// you should return 120 instead of 80 here.
return min(insertCounter, Int(dbmessages.count))
}

或者你可以像这样插入

tableView.insertSections(NSIndexSet(indexesIn: NSMakeRange(oldIns, minn)) as IndexSet, with: .top)

关于swift - 使用 Swift 在 TableView 中插入部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55334976/

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