Reputation: 2562
I have added some UIImage to my iOS app and wanted to show them in my custom UITableViewCell. There would be different images depending on what track is selected. So in my model, I have created the following code which is a switch statement.
class GrandPrix {
let grandPrix: String!
let firstDriver: String!
let secondDriver: String!
let thirdDriver: String!
var trackIconName = ""
init(grandPrix: String, firstDriver: String, secondDriver: String, thirdDriver: String){
self.grandPrix = grandPrix
self.firstDriver = firstDriver
self.secondDriver = secondDriver
self.thirdDriver = thirdDriver
}
func updateTrackicon(grandPrix: String) -> String {
switch (grandPrix) {
case "Australian GP":
return "Australian"
case "Bahrain GP":
return "Bahrain"
case "Chinese GP":
return "Chinese"
case "Azerbaijan GP":
return "Azerbaijan"
case "Spanish GP":
return "Chinese"
case "Monaco GP":
return "Monaco"
case "Canadian GP":
return "Canadian"
case "French GP":
return "French"
case "Austrian GP":
return "Austrian"
case "British GP":
return "British"
case "German GP":
return "German"
case "Hungarian GP":
return "Hungarian"
case "Belgian GP":
return "Belgian"
case "Italian GP":
return "Italian"
case "Singapore GP":
return "Singapore"
case "Russian GP":
return "Russian"
case "Japanese GP":
return "Japanese"
case "American GP":
return "United States"
case "Brazilian GP":
return "Brazilian"
case "Abu Dhabi GP":
return "Abu Dhabi"
default :
return "no track"
}
}
}
Now in my UITableViewClass I have included the following code that will show the UIImage within the cell
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! GrandPrixTableViewCell
let race = self.grandPrix[indexPath.row]
// Configure the cell...
cell.grandPrix.text = race.grandPrix
cell.firstPostion.text = race.firstDriver
cell.secondPosition.text = race.secondDriver
cell.thirdPostion.text = race.thirdDriver
cell.trackImage.image = UIImage(named: race.updateTrackicon(grandPrix: race.grandPrix))
return cell
}
I have hooked up the cell and checked that the size of the UIImages is well within the size of the UIImage in the storyboard. When I run the app the text appears but not the image. Any help would be appreciated.
Below is code that explains where the array is coming from.
import UIKit
class PredictorTableViewController: UITableViewController {
var predictorTrack = ""
var firstDriver = ""
var secondDriver = ""
var thirdDriver = ""
var grandPrix = [GrandPrix]() { didSet {self.tableView.reloadData()} }
I thought about this and tried to change the cellForRowAt indexPath
to the following code.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! GrandPrixTableViewCell
let race = self.grandPrix[indexPath.row]
// Configure the cell...
cell.grandPrix.text = race.grandPrix
cell.firstPostion.text = race.firstDriver
cell.secondPosition.text = race.secondDriver
cell.thirdPostion.text = race.thirdDriver
race.trackIconName = race.updateTrackicon(grandPrix: race.grandPrix)
cell.trackImage.image = UIImage(named: race.trackIconName)
return cell
}
I thought this would show the Track UIImage but unfortunately, I am still not getting an image.
Upvotes: 0
Views: 83