【问题标题】:How to assign value of type 'String?' to type 'UIImage?'如何分配“字符串”类型的值?输入“UIImage?”
【发布时间】:2021-01-31 08:15:37
【问题描述】:

我正在尝试从响应中加载图像,但它显示“无法分配 'String 类型的值?'输入'UIImage?'”。我已经在 tableView 中加载了标签,但无法加载图像。我使用 alamofire 进行 API 调用。这是我的代码。提前致谢

 extension ContactVC: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        arrData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell", for: indexPath) as? ContactCell
        cell?.lblEmpName.text = self.arrData[indexPath.row].name
        cell?.lblEmpDesignation.text = self.arrData[indexPath.row].designation
        cell?.imgEmp.image = self.arrData[indexPath.row].profilePhoto
        return cell!
    } 

【问题讨论】:

    标签: uitableview alamofireimage


    【解决方案1】:

    profilePhoto 似乎是一个 String,其中可能包含图像的 URL。因此,您必须先下载该图像,然后将其分配给单元格中的图像视图:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell", for: indexPath) as? ContactCell
        cell?.lblEmpName.text = self.arrData[indexPath.row].name
        cell?.lblEmpDesignation.text = self.arrData[indexPath.row].designation
        Alamofire.request(self.arrData[indexPath.row].profilePhoto).responseImage { response in
            if let image = response.result.value {
                cell?.imgEmp.image = image
            }
        }
        return cell!
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-08
      • 2018-10-11
      • 2021-09-25
      • 1970-01-01
      • 2021-04-03
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      相关资源
      最近更新 更多