Я боролся с этой проблемой в течение ЭОН, и я думал, что наконец-то нашел решение. Но нет. Я пытаюсь создать табличное представление, в котором изображения загружаются в правильные ячейки и сохраняются без мерцания.
Вот мое решение:
extension SearchViewController : BookDataProcessorDelegate {
func didFetchSmall(image: UIImage?, for indexPath: IndexPath, in tableView: UITableView) {
if tableView == resultsTableView {
if let cell = tableView.cellForRow(at: indexPath) as? BookTableViewCell {
cell.imageView?.image = image
}
}
}
func bookSearchDidFinish(with books: [Book]?) {
self.books = books == nil ? [] : Array(books!.prefix(MAX_BOOK_COUNT))
resultsTableView.reloadData()
}
}
extension SearchViewController : UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.books.count
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
BookDataProcessor.shared.fetchSmallImage(for: books[indexPath.row], at:indexPath, in:resultsTableView)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "bookCell", for: indexPath) as! BookTableViewCell
cell.authorLabel.text = books[indexPath.row].authorName?[0] ?? ""
cell.titleLabel.text = books[indexPath.row].title ?? ""
cell.delegate = self
cell.indexPath = indexPath
return cell
}
Это довольно очевидно. По сути, BookDataProcessor.shared.fetchSmallImage
запускает метод didFetchSmall
, который должен установить правильную ячейку tableView. Однако клетки мерцают и исчезают в половине случаев. Мне чрезвычайно интересно узнать, что я делаю неправильно.