我已将一些UIImage添加到我的iOS应用中,并希望在我的自定义UITableViewCell中显示它们。根据选择的音轨,会有不同的图像。因此,在我的模型中,我创建了以下代码,这是一个switch语句。
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"
}
}
}
现在,在UITableViewClass中,我包含了以下代码,这些代码将在单元格中显示UIImage
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
}
我已经连接好单元格,并检查UIImages的大小是否在情节提要中的UIImage的大小内。当我运行该应用程序时,将显示文本,但不显示图像。任何帮助将不胜感激。
下面是解释数组来源的代码。
import UIKit
class PredictorTableViewController: UITableViewController {
var predictorTrack = ""
var firstDriver = ""
var secondDriver = ""
var thirdDriver = ""
var grandPrix = [GrandPrix]() { didSet {self.tableView.reloadData()} }
我考虑了这一点,并尝试将cellForRowAt indexPath
更改为以下代码。
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
}
我认为这会显示Track UIImage,但不幸的是,我仍然没有得到图像。
答案 0 :(得分:1)
这可能是命名错误。确认名称是否匹配。