gpt4 book ai didi

swift - '无效更新 : invalid number of rows in section 1

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

我正在尝试对 tableView 中的聊天重新排序。最近更新的聊天应该插入到 IndexPath(row: 0, section: 1)。我的理解是,为了删除一行,它必须是可见的。

如果行不可见,当数据源更新时,我只需要 tableView.insertRows(at: [newIndex], with: .fade)

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (17) must be equal to the number of rows contained in that section before the update (17), 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).'

//chats of the currentUser
var currentUserChats = [Chat]() {
didSet(newValue){
attachChildChangedObserverOn(chat: newValue)
}
}

var observersArray = [String: UInt]() // chatUID: handle


//attach childChange listener on each chat downloaded
func attachChildChangedObserverOn(chat: Chat) {


var handle: UInt = 0
let ref = DDatabaseRReference.chats.reference().child(chat.chatUID).child("users").child(currentUser.userUID)


handle = ref.observe(.childChanged, with: {[weak self] (snapshot) in

self?.observersArray[chat.chatUID] = handle

guard snapshot.exists() else {return}

let chatChanged = chat

var lastMessage = ""
var unreadMessagesCount = 0
var lastUpdate = 0.0

switch snapshot.key {

case "lastMessage" :
lastMessage = snapshot.value as! String
chatChanged.lastMessage = lastMessage

case "unreadMessagesCount":
unreadMessagesCount = snapshot.value as! Int
if let index = chatChanged.users.index(of: (self?.currentUser)!) {
let userChanged = chatChanged.users[index]
userChanged.unreadMessagesCount = unreadMessagesCount

chatChanged.users.remove(at: index)
chatChanged.users.insert(userChanged, at: index)
}

case "lastUpdate":
lastUpdate = snapshot.value as! Double
chatChanged.lastUpdate = lastUpdate

default: return
}


let newIndex = IndexPath(row: 0, section: 1)

// get indexOf chatChanged
guard let index = self?.currentUserChats.index(of: chatChanged) else {return}
let indexPathOfOldChat = IndexPath(row: index, section: 1)

// - update Data Source
// - reloadRow
if indexPathOfOldChat.row == 0 {
self?.currentUserChats.remove(at: 0)
self?.currentUserChats.insert(chatChanged, at: 0)
self?.tableView.reloadRows(at: [newIndex], with: .fade)
return
}


//get visible indexes of cells in TableView
let visibleIndexes = self?.tableView.indexPathsForVisibleRows

//check if the index of chat to be updated is visible
if let indexes = visibleIndexes,
indexes.contains(indexPathOfOldChat) {

//index is visible
// update Data Source, delete row & insert row
self?.tableView.beginUpdates()
self?.currentUserChats.remove(at: indexPathOfOldChat.row)
self?.tableView.deleteRows(at: [indexPathOfOldChat], with: .fade)

self?.currentUserChats.insert(chatChanged, at: 0)
self?.tableView.insertRows(at: [newIndex], with: .fade)
self?.tableView.endUpdates()
return
}

//if index is not visible:
// - update Data Source
// - insert the row
self?.currentUserChats.remove(at: index)
self?.currentUserChats.insert(chatChanged, at: 0)

self?.tableView.beginUpdates()
self?.tableView.insertRows(at: [newIndex], with: .fade)
self?.tableView.endUpdates()
return
})
}

最佳答案

事实证明,不可见的行可以删除。
最初,我以为如果我试图删除一个不可见的行,我会得到一个错误,因为该行的单元格将不再出现在 View 中。

   //if index is not visible:
// - update Data Source
// - DELETE the previous row
// - insert the new row

self?.currentUserChats.remove(at: indexRow)
self?.currentUserChats.insert(chatChanged, at: 0)

self?.tableView.beginUpdates()
self?.tableView.deleteRows(at: [indexPathOfOldChat], with: .none)
self?.tableView.insertRows(at: [newIndex], with: .fade)
self?.tableView.endUpdates()
return

关于swift - '无效更新 : invalid number of rows in section 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53061782/

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