如何修复用于条件绑定的Initializer必须具有Optional类型,而不是'String'?

时间:2019-03-29 14:28:24

标签: ios swift

如何解决此问题? 条件绑定的初始化程序必须具有可选类型,而不是'String'

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

    cell.lblNamnEfternamn.text = personsArray[indexPath.row].wholeName
    cell.lblEmail.text = personsArray[indexPath.row].email
    cell.imageAvatar.image = UIImage(named: "avatar")

    if let newProfileImageUrl = personsArray[indexPath.row].profileImageUrl {
        let url = NSURL(string: newProfileImageUrl)
        let request = URLRequest(url: url! as URL)
        let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
            if error != nil {
                print(error!)
                return
            }

            DispatchQueue.main.async {
                cell.imageAvatar.image = UIImage(data: data!)
                tableView.reloadData()
            }

        }
    }
    return cell
}

enter image description here

1 个答案:

答案 0 :(得分:0)

替换此

imageView.roundedImage() 

使用

if let newProfileImageUrl = personsArray[indexPath.row].profileImageUrl {

另外,最好使用SDWebImage

let newProfileImageUrl = personsArray[indexPath.row].profileImageUrl 

而不是每次滚动都重复获取图像