2

How can I solve this problem? Initializer for conditional binding must have Optional type, not '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

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Genti Malm
  • 23
  • 1
  • 1
  • 5

1 Answers1

3

Replace this

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

with

let newProfileImageUrl = personsArray[indexPath.row].profileImageUrl 

Also it's better to use SDWebImage

cell.imageAvatar.sd_setImage(with: URL(string:newProfileImageUrl), placeholderImage: UIImage(named: "placeholder.png"))

instead of repeatedly fetching the image every scroll

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87