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

标签 swift uitableview tableview

我正在尝试对 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/

相关文章:

swift - 字符串数组排序

iOS:在 tableView 中显示 firebase 数据

ios - 如何从 PencilKit 中的 PKInkingTool 设置笔画的恒定宽度

ios - 如何在 IOS 的 Action Extension 中制作启动画面

ios - Pulltorefresh 只工作一次而不是多次

swift - 使用 autoID 从 Firebase 获取数据 - Xcode

Javafx - 我无法将数据放入 TableView

ios - "Code signing "GoogleToolboxForMac.framework "failed"上传到 Itunes Connect 时

swift - 通过函数设置内部时,如何再次调用didSet?

uitableview - Swift - 在自定义单元格中添加长按手势识别器