gpt4 book ai didi

ios - 如何在 Swift 中的 TableView Controller 中将不同的图像放在文本旁边?

转载 作者:行者123 更新时间:2023-11-30 11:51:20 25 4
gpt4 key购买 nike

我遇到一个问题,我已经放置了“快餐”的图像,并且它显示在我的 TableView Controller 上的所有文本上。我想展示的是“快餐”、“餐饮”、“服务站”等,它们在表格 View Controller 上有自己的图像。我尝试实现它,但我需要一些帮助。有人可以指导我吗?这是我的代码:

class NearMeIndexTitle {
var indexTitle = String()
var imageName = String ()
}

class NearMeIndexTableViewController: UITableViewController {
var nearMeIndexes = [NearMeIndexTitle]()

override func viewDidLoad() {
super.viewDidLoad()

// Table view list ..
let nearmeindex1 = NearMeIndexTitle()
let nearmeindex1Img = NearMeIndexTitle()

nearmeindex1.indexTitle = "Fast Food"
nearmeindex1.imageName = "fastfood.png"

let nearmeindex2 = NearMeIndexTitle()
nearmeindex2.indexTitle = "Dining"

let nearmeIndex3 = NearMeIndexTitle()
nearmeIndex3.indexTitle = "Service Stations"

let nearmeIndex4 = NearMeIndexTitle()
nearmeIndex4.indexTitle = "Hospitals "

let nearmeIndex5 = NearMeIndexTitle()
nearmeIndex5.indexTitle = "Grocery"

nearMeIndexes.append(nearmeindex1)
nearMeIndexes.append(nearmeindex2)
nearMeIndexes.append(nearmeIndex3)
nearMeIndexes.append(nearmeIndex4)
nearMeIndexes.append(nearmeIndex5)

self.tableView.reloadData()
}

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

let nearMendexCellValue = nearMeIndexes[indexPath.row]
cell.nearMeLabel.text = nearMendexCellValue.indexTitle

cell.nearMeImageView.image = UIImage(named: nearMendexCellValue.imageName1)

return cell
}
}

最佳答案

已经对代码进行了更改,您需要设置其他类型的图像名称,我已经标记了。

class NearMeIndexTableViewController: UITableViewController  {
var nearMeIndexes = [NearMeIndexTitle]()

override func viewDidLoad() {
super.viewDidLoad()

// Table view list ..
let nearmeindex1 = NearMeIndexTitle()
let nearmeindex1Img = NearMeIndexTitle()

nearmeindex1.indexTitle = "Fast Food"
nearmeindex1.imageName = "fastfood.png"

let nearmeindex2 = NearMeIndexTitle()
nearmeindex2.indexTitle = "Dining"
nearmeindex2.imageName = "dining.png" // here

let nearmeIndex3 = NearMeIndexTitle()
nearmeIndex3.indexTitle = "Service Stations"
nearmeindex3.imageName = "stations.png" // here

let nearmeIndex4 = NearMeIndexTitle()
nearmeIndex4.indexTitle = "Hospitals "
nearmeindex4.imageName = "hospitals.png" // here

let nearmeIndex5 = NearMeIndexTitle()
nearmeIndex5.indexTitle = "Grocery"
nearmeindex5.imageName = "grocery.png" // here

nearMeIndexes.append(nearmeindex1)
nearMeIndexes.append(nearmeindex2)
nearMeIndexes.append(nearmeIndex3)
nearMeIndexes.append(nearmeIndex4)
nearMeIndexes.append(nearmeIndex5)

self.tableView.reloadData()
}

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

let nearMendexCellValue = nearMeIndexes[indexPath.row]
cell.nearMeLabel.text = nearMendexCellValue.indexTitle

cell.nearMeImageView.image = UIImage(named: nearMendexCellValue.imageName1)

return cell
}
}

关于ios - 如何在 Swift 中的 TableView Controller 中将不同的图像放在文本旁边?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48292741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com