Ошибка при перезагрузке TableView (удалить строку перед обновлением) - PullRequest
0 голосов
/ 12 июня 2019

Это ошибка ...

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 4 from section 0 which only contains 0 rows before the update'

И код, при котором возникает эта ошибка, следующий:

cell.imageView?.sd_setImage(with: fileUrl as URL?, placeholderImage: placeholderImage,
                                    options: [],
                                    progress: nil,
        completed: { (image, error, cacheType, imageURL) in

            guard let image = image else { return }
            //print("Image arrived!")
            cell.imageView?.image = self.resizeImage(image: image, newWidth: 50)

            let indexPath = NSIndexPath(row: self.usersArray.count-1, section: 0)

            self.tableview.reloadRows(at: [indexPath as IndexPath], with: .none)

            cell.isHidden = false

        })

Когда я удаляю эти две строки, ошибка не возникает

let indexPath = NSIndexPath(row: self.usersArray.count-1, section: 0)

self.tableview.reloadRows(at: [indexPath as IndexPath], with: .none)

Но мне нужно перезагрузить TableView после загрузки изображений в ячейки.

Как это можно решить? Надеюсь, вы поможете мне, большое спасибо.

Ответы [ 2 ]

1 голос
/ 12 июня 2019

Я думаю, что это кусок кода в ViewController. Может быть, это может сработать

cell.imageView?.sd_setImage(with: fileUrl as URL?, placeholderImage: placeholderImage,
                                options: [],
                                progress: nil,
    completed: { [weak self] (image, error, cacheType, imageURL) in
        guard let 'self' = self else { return }
        guard let image = image else { return }
        //print("Image arrived!")
        cell.imageView?.image = self.resizeImage(image: image, newWidth: 50)

        DispatchQueue.main.async {
          let indexPath = IndexPath(row: self.usersArray.count-1, section: 0)

          self.tableview.reloadRows(at: [indexPath], with: .none)
        }
        cell.isHidden = false

    })
0 голосов
/ 12 июня 2019

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

__tableView.beginUpdates()
let rowToReload = IndexPath(row: usersArray.count - 1, section: 0)
__tableView.reloadRows(at: [rowsToReload], with: .none)
__tableView.endUpdates()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...