'Неверное обновление: неверное количество строк в разделе 1 - PullRequest
0 голосов
/ 30 октября 2018

Я пытаюсь изменить порядок чатов в табличном представлении. Последний обновленный чат должен быть вставлен в IndexPath(row: 0, section: 1). Насколько я понимаю, чтобы удалить строку, она должна быть видимой.

Если строка не видна, при обновлении источника данных мне просто нужно tableView.insertRows(at: [newIndex], with: .fade).

*** Завершение приложения из-за необработанного исключения «NSInternalInconsistencyException», причина: «Неверное обновление: недействительно количество строк в разделе 1. Количество строк, содержащихся в существующий раздел после обновления (17) должен быть равен количеству строки, содержащиеся в этом разделе до обновления (17), плюс или минус количество строк, вставленных или удаленных из этого раздела (1 вставлено, 0 удалено) и плюс или минус количество строк, перемещенных в или из этот раздел (0 переместился, 0 переместился). '

//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
    })
}

1 Ответ

0 голосов
/ 30 октября 2018

Оказывается, что строки, которые не видны, могут быть удалены.
Сначала я думал, что получу ошибку, если попытаюсь удалить строку, которая не была видна, потому что эта ячейка для этой строки больше не будет в представлении.

   //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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...