-1
import UIKit
struct Headline {


var id : Int
var title : String
var text : String
var image : String
var color: UIColor!


}
var headlines = [
            [
                Headline(id: 1, title: "Mazda 3 2.0", text: "Price: 9800000000", image: "", color: UIColor.red),
                Headline(id: 2, title: "Mazda 3 2.0", text: "Price: 9600000000", image: "", color: UIColor.blue),
                Headline(id: 3, title: "Mazda 3 2.0", text: "Price: 9000000000", image: "", color: UIColor.black)
            ],
            [
                Headline(id: 4, title: "Mazda 3 1.5", text: "Price: 8000000000", image: "", color: UIColor.red),
                Headline(id: 5, title: "Mazda 3 1.5", text: "Price: 8500000000", image: "", color: UIColor.blue),
                Headline(id: 6, title: "Mazda 3 1.5", text: "Price: 8700000000", image: "", color: UIColor.black)
            ]


    ]

class CarTableViewController: UITableViewController {


override func numberOfSections(in tableView: UITableView) -> Int {
    return headlines.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return headlines[section].count
}

let headerTitles = ["Mazda Version 2", "Mazda Version 1.5"]
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section < headerTitles.count {
        return headerTitles[section]
    }

    return nil
}

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

    let headline = headlines[indexPath.row]
    cell.textLabel?.text = headline.title
    cell.textLabel?.textColor = UIColor.white
    cell.detailTextLabel?.text = headline.text
    cell.detailTextLabel?.textColor = UIColor.white
    cell.backgroundColor = headline.color
    cell.imageView?.image = UIImage(named: headline.image)


    return cell
}

}

I had 3 link: https://f1.media.brightcove.com/8/1078702682/1078702682_6004950245001_6004956161001-vs.jpg?pubId=1078702682&videoId=6004956161001 https://c.ndtvimg.com/2019-08/k8519lf8_bugatti-centodieci-unveiled-at-pebble-beach-car-show_625x300_17_August_19.jpg https://cdn.motor1.com/images/mgl/17XG3/s1/bugatti-la-voiture-noire-leaves-geneva.jpg

How to use URLSesstion set image for tableviewcontroll ???

PGDev
  • 23,751
  • 6
  • 34
  • 88
Tan TN
  • 21
  • 5
  • 1
    Possible duplicate of [Loading/Downloading image from URL on Swift](https://stackoverflow.com/questions/24231680/loading-downloading-image-from-url-on-swift) – Shamas S Sep 10 '19 at 09:46

1 Answers1

0

In cellForRowAt, set the image for urlString like,

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //rest of the code...
    if let url = URL(string: headline.image) {
        URLSession.shared.dataTask(with: url) { (data, response, error) in
            if let data = data {
                let image = UIImage(data: data)
                DispatchQueue.main.async {
                    cell.imageView?.image = image
                }
            }
        }.resume()
    }
    return cell
}

Edit:

Update the headlines array to,

var headlines = [
    [
        Headline(id: 1, title: "Mazda 3 2.0", text: "Price: 9800000000", image: "https://cdn.motor1.com/images/mgl/17XG3/s1/bugatti-la-voiture-noire-leaves-geneva.jpg", color: UIColor.red),
        Headline(id: 2, title: "Mazda 3 2.0", text: "Price: 9600000000", image: "https://c.ndtvimg.com/2019-08/k8519lf8_bugatti-centodieci-unveiled-at-pebble-beach-car-show_625x300_17_August_19.jpg", color: UIColor.blue),
        Headline(id: 3, title: "Mazda 3 2.0", text: "Price: 9000000000", image: "https://f1.media.brightcove.com/8/1078702682/1078702682_6004950245001_6004956161001-vs.jpg?pubId=1078702682&videoId=6004956161001", color: UIColor.black)
    ],
    [
        Headline(id: 4, title: "Mazda 3 1.5", text: "Price: 8000000000", image: "https://cdn.motor1.com/images/mgl/17XG3/s1/bugatti-la-voiture-noire-leaves-geneva.jpg", color: UIColor.red),
        Headline(id: 5, title: "Mazda 3 1.5", text: "Price: 8500000000", image: "https://c.ndtvimg.com/2019-08/k8519lf8_bugatti-centodieci-unveiled-at-pebble-beach-car-show_625x300_17_August_19.jpg", color: UIColor.blue),
        Headline(id: 6, title: "Mazda 3 1.5", text: "Price: 8700000000", image: "https://f1.media.brightcove.com/8/1078702682/1078702682_6004950245001_6004956161001-vs.jpg?pubId=1078702682&videoId=6004956161001", color: UIColor.black)
    ]    
]
PGDev
  • 23,751
  • 6
  • 34
  • 88
  • this isn't the correct approach cells are re-used and this code may download the same image many times even at the same time – Shehata Gamal Sep 10 '19 at 09:49
  • I want each cell to be a different image :( – Tan TN Sep 10 '19 at 09:55
  • @TrầnNgọcTân Have you added the images in your Headline objects? If yes, it will fetch the image for each headline.image. – PGDev Sep 10 '19 at 09:56
  • i foget delete iamges in obj headline, now, i want get 3 image from 3 link, how can i get this, pls help me ty – Tan TN Sep 10 '19 at 10:00
  • Where have you stored the image urls? First update the code. And the code I provided will work for each cell. Atleast give it a try first and see what error are you getting. – PGDev Sep 10 '19 at 10:01
  • 3 url in question, i updated code but i dont know to where URL – Tan TN Sep 10 '19 at 10:06
  • Here `Headline(id: 1, title: "Mazda 3 2.0", text: "Price: 9800000000", image: "", color: UIColor.red)` the image is empty string. Instead add the actual urlString in all these. – PGDev Sep 10 '19 at 10:09
  • i tried, no error, but when running the app the image does not display – Tan TN Sep 10 '19 at 10:13