ios - 第一个 TableView 单元格没有正确出队

标签 ios swift concurrency tableview alamofire

我正在开发应用程序,它使用 https://www.countryflags.io/ 将标志加载到类中应用程序接口(interface)。我在使用 Alamofire get 请求初始化对象时加载了一个标志。 问题是在启动应用程序时加载的前几个 TableView 单元格没有标志。

But when I scroll back after scrolling down, they load perfectly.

我认为它正在发生,因为请求处理得不够快,并且第一个标志在单元格出队开始之前还没有准备好加载。但我不知道如何在 getFlag() 方法中设置一些东西来帮助我在完成时重新加载 TableView 数据或延迟出列到加载所有标志的时间点。

带有 getflag() 方法的国家类

import UIKit
import Alamofire

final class Country {
    let name: String
    let code: String
    var flag: UIImage?
    var info: String?

    init(name: String, code: String, flag: UIImage? = nil, info: String? = nil) {
        self.name = name
        self.code = code
        if flag == nil {
            getFlag()
        } else {
            self.flag = flag
        }
        self.info = info
    }

    func getFlag() {
        let countryFlagsURL = "https://www.countryflags.io/\(code.lowercased())/shiny/64.png"

        Alamofire.request(countryFlagsURL).responseData { response in
            if response.result.isSuccess {
                if let data = response.data {
                    self.flag = UIImage(data: data)
                }
            }
        }
    }
}

cellForRowAt 方法

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let country = countries[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "Country", for: indexPath)

        cell.textLabel?.text = country.name
        if let flag = country.flag {
            cell.imageView?.image = flag
        } else {
            cell.imageView?.image = .none
        }

        return cell
    }

最佳答案

Countryinit 方法不应启动异步图像检索。相反,您应该让 cellForRowAt 启动异步图像检索。

但是您也不应该只是愉快地异步更新 cell(因为在您的 Alamofire 请求完成时该行可能已被重用)。而且,更微妙的一点是,如果您快速滚动到 TableView 的末尾,您将希望避免图像请求积压,因此您希望取消对不再可见的行的未决请求。有多种方法可以实现所有这三个目标(cellForRowAt 中的异步图像检索,在单元格已用于另一行后不要更新单元格,如果出现问题则不要让它积压快速滚动)。

最简单的方法是使用 AlamofireImage .然后,所有处理异步图像请求、取消重用单元格请求等的复杂性都将简化为:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: “Country", for: indexPath)
    cell.imageView.af_setImage(withURL: objects[indexPath.row].url, placeholderImage: placeholder)
    return cell
}

请注意,如果您要使用默认的表格 View 单元格,我建议您在此例程中提供一个占位符图像,如上所示。只需创建一个与您的标志大小相同的空白图像(或其他)。这样可以确保单元格的布局正确。


顺便说一句,如果您想以编程方式生成该占位符图像,您可以执行以下操作:

let imageSize = CGSize(width: 44, height: 44)

lazy var placeholder: UIImage = UIGraphicsImageRenderer(size: imageSize).image { _ in
    UIColor.blue.setFill()
    UIBezierPath(rect: CGRect(origin: .zero, size: imageSize)).fill()
}

现在,这会创建一个 44×44 的蓝色占位符缩略图,但您可以根据您的应用调整颜色和大小。

关于ios - 第一个 TableView 单元格没有正确出队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57227901/

相关文章:

java - 同步线程安全对象列表中的多个读取和写入

ios - 无法启动 UICollectionViewController

swift - parse.com 中的字典字段

ios - 使用未声明的标识变量

ios - 确保在发送其他请求之前完成初始网络请求的最佳方法(iOS 应用程序)

arrays - 按键对字典进行排序并将其转换为数组

java - 在Java中是否可以等待不在线程中的方法完成?

java - Java中的并发读/写缓冲区

ios - 将 AdMob 添加到 Firebase 项目时崩溃

ios - 有没有办法在构建之前下载配置文件(使用 Jenkins 或脚本)来构建机器?