0

Whenever I tried to fetch the data from firebase this happen

enter image description here

This is my code

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 }

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

    cell.postedImage.downloadImageUrl(from: setPost[indexPath.section].userPostImage)
    cell.postItemPriceLabel.text = setPost[indexPath.section].userPriceTag
    cell.selectionStyle = .none

    return cell
}

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let cell = tableView.dequeueReusableCell(withIdentifier: self.headerCell) as! HeaderCell
    cell.profileImage.downloadImageUrl(from: setPost[section].userphoto)
    cell.fullname.text = setPost[section].fullname
    cell.backgroundColor = UIColor.white
    return cell
}


 extension UIImageView {
   func downloadImageUrl(from imgUrl: String!){

    let url = URLRequest(url: URL(string: imgUrl)!)

    let session = URLSession.shared

    session.dataTask(with: url){
        (data, response, err) in

        if err != nil {
            print(err!)
            return
        }
        DispatchQueue.main.async {
            self.image = UIImage(data: data!)
        }
        }.resume()

Please someone should help me out

Thanks

Malik
  • 921
  • 11
  • 12

1 Answers1

3

You need to make sure that the string used to create a URL object (imgUrl) can be used to create a URL object, if it doesn't, you shouldn't continue.

This can be made by using an if let statement:

if let url = URL(string: imgUrl) {

    let urlRequest = URLRequest(url: url)

    let session = URLSession.shared

    session.dataTask(with: urlRequest){
        (data, response, err) in

        if err != nil {
            print(err!)
            return
        }
        DispatchQueue.main.async {
            self.image = UIImage(data: data!)
        }
        }.resume()
}
Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36