You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift MVVM模式下Collection View Cell加载网络图片不显示问题求助

Swift MVVM模式下Collection View Cell加载网络图片不显示问题求助

我现在正试着从指定URL加载图片,并把它显示在Collection View的Cell中,采用的是MVVM架构。图片的URL是https://picsum.photos/200,这个链接会返回单张图片。我已经给Collection View设置好了代理和数据源,但就是找不到图片无法显示的原因。

下面是我的网络层代码:

import Foundation
import UIKit

enum ImageFetchingError: Error {
    case timeout
    case unknown
}

protocol CatImageCellModel {
    func fetchCatImage(completion: @escaping (Result<UIImage, ImageFetchingError>) -> Void)
}

class NetworkLayer: CatImageCellModel  {
    
    func fetchCatImage(completion: @escaping (Result<UIImage, ImageFetchingError>) -> Void) {
        
        let url = "https://picsum.photos/200"
        guard let _url = URL(string: url) else {
            return
        }
        
        URLSession.shared.dataTask(with: _url) { data, response, error in
            // 原代码未完整粘贴,此处保留已输入部分
        }
    }
}

备注:内容来源于stack exchange,提问作者user23509100

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 14:13:08