如何解决这个问题? 条件绑定(bind)的初始化程序必须具有可选类型,而不是“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
}
最佳答案
替换这个
if let newProfileImageUrl = personsArray[indexPath.row].profileImageUrl {
与
let newProfileImageUrl = personsArray[indexPath.row].profileImageUrl
此外,最好使用 SDWebImage
cell.imageAvatar.sd_setImage(with: URL(string:newProfileImageUrl), placeholderImage: UIImage(named: "placeholder.png"))
而不是每次滚动时重复获取图像
关于ios - 如何修复条件绑定(bind)的初始化程序必须具有可选类型,而不是 'String' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55419596/