简体   繁体   中英

TableView assigning image to cell in TVC

Having a problem with this code. Basically i'm trying to populate a table cell using an image im pulling from twitter. The url field here has the value http://pbs.twimg.com/profile_images/796924570150301696/35nSG5nN_normal.jpg but for some reason the print("REACHED") is never printed. Any help/suggestions appreciated!

code snippet:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: tIdentifier, for: indexPath) as! TweetCell
    let tweet = tweets[indexPath.section][indexPath.row]
    let url = tweet.user.profileImageURL!
    print(url.absoluteString)
    let data = try? Data(contentsOf: url)
    if (data == nil){
    } else {
        print("REACHED")
        cell.avatarImage = UIImage(data: data!)
    }
    cell.tweet = tweets[indexPath.section][indexPath.row]
    return cell
}

This worked for me:

func example() {
    let cell = UITableViewCell()
    let url = URL(string: "http://pbs.twimg.com/profile_images/796924570150301696/35nSG5nN_normal.jpg")

    do {
        let data = try Data(contentsOf: url!)
        print("REACHED")
        cell.imageView?.image = UIImage(data: data)
    } catch {
        print("received this error:\n\(error.localizedDescription)")
    }
}

If it doesn't work right away, at least you'll have an error message to help you figure it out. Good luck!

Edit: You should make sure you have updated your Info.plist to include an entry for:

App Transport Security Settings

Without this you will not have access to other sites. Transport security has blocked a cleartext HTTP

Some tips for an easy life…

  • Don't force unwrap
  • Don't download on the main queue
  • Don't expose your cell's IBOutlet s

let imageQueue = DispatchQueue(label: "imageQueue", qos: DispatchQoS.background)

class TweetCell: UITableViewCell {

    @IBOutlet fileprivate var avatarImage: UIImageView!

    var tweet: Tweet {        
        didSet {
            guard let url = tweet.user.profileImageURL else { return }

            loadImage(url: url)
        }
    }

    fileprivate func loadImage(url: URL) {
        imageQueue.async {

            do {
                let data = try Data(contentsOf: url) 

                DispatchQueue.main.async {
                    self.avatarImage.image = UIImage(data: data)        
                }

            } catch {
                // Handle error
            }    
        }
    }
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: tIdentifier, for: indexPath) as! TweetCell
    cell.tweet = tweets[indexPath.section][indexPath.row]
    return cell
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM