CellForRowAt:即使单元格可见,也返回nil

时间:2017-07-18 13:05:54

标签: ios swift uitableview

在我的项目中,我有tableView ImageView,其中图片是从互联网上下载的。

由于下载可能需要一段时间,在cellForRowAtIndexPath中,我会在显示之前检查单元格是否仍然可见,这是为了确保图像不会显示在错误的单元格中。

但是,由于我对代码进行了更改,我认为不应该更改任何内容,即使单元格可见,cellForRowAtIndexPath函数也会返回nil

这是我的工作代码:

ExcursionImageDownloader.shared.getImage(from: url, completion: {(image) in
                if let updateCell = tableView.cellForRow(at: indexPath) as? ExcursionTableCell {
                    updateCell.thumbnailImage.image = image
                    print("Image View size is: \(updateCell.thumbnailImage.frame.size)")
                }
            })

每当图像下载完成后,getImage都会调用完成。在我声明updateCell的第二行中,我确保单元格仍然可见,并且它完美地运行。

现在我将代码更改为以下内容:

ExcursionImageDownloader.shared.thumbnailImage(from: currentExcursion, url: url, completion: { image in
                print(tableView.indexPathsForVisibleRows)
                print(indexPath)
                if let updateCell = tableView.cellForRow(at: indexPath) as? ExcursionTableCell {
                    print("Displaying image")
                    updateCell.thumbnailImage.image = image
                }
            })

再次完成处理程序被调用没有问题,但是,updateCell现在总是为零,因此thumbnailImage永远不会被分配。为了确保单元格可见,我添加了2个print语句,这是输出:

Optional([[0, 0], [1, 0], [2, 0]])
[0, 0]
Optional([[0, 0], [1, 0], [2, 0]])
[1, 0]
Optional([[0, 0], [1, 0], [2, 0]])
[2, 0]

正如您所看到的那样,单元格清晰可见,我完全不知道为什么cellForRowAtIndexPath会返回nil。在我看来,这可能是一个错误,但是,我不确定。

我的代码中是否存在错误,或者这实际上是一个错误?

更新

这是完整的cellForRowAt函数:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //Safely downcast cell, if it's not existing due to any reason it will create and return an empty cell
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "ExcursionCell", for: indexPath) as? ExcursionTableCell else {return UITableViewCell()}

        //Since we are working with sections that have different amount of cells, we will get the correct excursion for this cell from the excursions array that is nested inside the excursionsSections array.
        let currentExcursion = self.excursionsSections[ indexPath.section].excursions[indexPath.item]


        cell.title.text = currentExcursion.title
        cell.descriptionText.text = currentExcursion.thumbnailDescription
        cell.thumbnailImage.image = #imageLiteral(resourceName: "PlaceHolder").withRenderingMode(.alwaysTemplate)

        //Customize the cell
        cell.descriptionText.numberOfLines = 0
        cell.thumbnailImage.tintColor = UIColor.primary

        //Download image 

        if currentExcursion.imageURLs.count > 0 {
            let url = createURL(withImage: currentExcursion.imageURLs[0], width: thumbnailSize().width, height: thumbnailSize().height)

            ExcursionImageDownloader.shared.thumbnailImage(from: currentExcursion, url: url, completion: { image in
                print(tableView.indexPathsForVisibleRows)
                print(indexPath)
                if let updateCell = tableView.cellForRow(at: indexPath) as? ExcursionTableCell {
                    updateCell.thumbnailImage.image = image
                }
            })

        setColor(selected: false, cell: cell)

        return cell
    }

0 个答案:

没有答案