gpt4 book ai didi

快速 TableView 将 reloadData 更改为 insertRows

转载 作者:行者123 更新时间:2023-11-30 10:48:11 30 4
gpt4 key购买 nike

尝试更改方法来更新数据,因为 reloadData 有滞后

let oldIns = insertCounter
insertCounter += Int(INSERT_MESSAGES) // +40
var indexPaths = [IndexPath]()
for section in (oldIns..<insertCounter) {
indexPaths.append(IndexPath(row: 2, section: section))
}
tableView.beginUpdates()
tableView.insertRows(at: indexPaths, with: .automatic)
tableView.endUpdates()

但我有错误

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 (0 inserted, 0 deleted)

func numberOfSections(in tableView: UITableView) -> Int {

return min(insertCounter, Int(dbmessages.count))
}

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

return 5
}

//---------------------------------------------------------------------------------------------------------------------------------------------
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

return RCMessages().sectionHeaderMargin
}

//---------------------------------------------------------------------------------------------------------------------------------------------
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

return RCMessages().sectionFooterMargin
}

//---------------------------------------------------------------------------------------------------------------------------------------------
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

view.tintColor = UIColor.clear
}

//---------------------------------------------------------------------------------------------------------------------------------------------
func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {

view.tintColor = UIColor.clear
}

//---------------------------------------------------------------------------------------------------------------------------------------------
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

if (indexPath.row == 0) {
return RCSectionHeaderCell.height(indexPath, messagesView: self)
}

if (indexPath.row == 1) {
return RCBubbleHeaderCell.height(indexPath, messagesView: self)
}

if (indexPath.row == 2) {

let rcmessage = self.rcmessage(indexPath)
if (rcmessage.type == RC_TYPE_STATUS) { return RCStatusCell.height(indexPath, messagesView: self) }
if (rcmessage.type == RC_TYPE_TEXT) { return RCTextMessageCell.height(indexPath, messagesView: self) }

}

if (indexPath.row == 3) {
return RCBubbleFooterCell.height(indexPath, messagesView: self)
}

if (indexPath.row == 4) {
return RCSectionFooterCell.height(indexPath, messagesView: self)
}
return 0
}

//---------------------------------------------------------------------------------------------------------------------------------------------
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

if (indexPath.row == 0) {
let cell = tableView.dequeueReusableCell(withIdentifier: "RCSectionHeaderCell", for: indexPath) as! RCSectionHeaderCell
cell.bindData(indexPath, messagesView: self)
return cell
}

if (indexPath.row == 1) {
let cell = tableView.dequeueReusableCell(withIdentifier: "RCBubbleHeaderCell", for: indexPath) as! RCBubbleHeaderCell
cell.bindData(indexPath, messagesView: self)
return cell
}

if (indexPath.row == 2) {

let rcmessage = self.rcmessage(indexPath)
if (rcmessage.type == RC_TYPE_STATUS) {
let cell = tableView.dequeueReusableCell(withIdentifier: "RCStatusCell", for: indexPath) as! RCStatusCell
cell.bindData(indexPath, messagesView: self)
return cell
}

if (rcmessage.type == RC_TYPE_TEXT) {
let cell = tableView.dequeueReusableCell(withIdentifier: "RCTextMessageCell", for: indexPath) as! RCTextMessageCell
cell.bindData(indexPath, messagesView: self)

let numSections = self.tableView.numberOfSections
if numSections == 1 {
updateTableContentInset()
}
return cell
}

}

if (indexPath.row == 3) {
let cell = tableView.dequeueReusableCell(withIdentifier: "RCBubbleFooterCell", for: indexPath) as! RCBubbleFooterCell
cell.bindData(indexPath, messagesView: self)
return cell
}

if (indexPath.row == 4) {
let cell = tableView.dequeueReusableCell(withIdentifier: "RCSectionFooterCell", for: indexPath) as! RCSectionFooterCell
cell.bindData(indexPath, messagesView: self)
return cell
}

return UITableViewCell()
}

如何正确在tableview中插入新行?以前是这样的

insertCounter += Int(INSERT_MESSAGES)
tableView.reloadData()

最佳答案

这是一个非常简单的代码行:

 tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .top)

这将在 TableView 的顶部插入新行。然后您将重新加载数据。这应该可以解决问题。

关于快速 TableView 将 reloadData 更改为 insertRows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55329922/

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