我遇到了一个问题,我为“快餐”放了一个图像,它显示在我的桌面视图控制器上的所有文本上。我要展示的只是“快餐”,“餐饮”,“服务站”等,它们在桌面视图控制器上有自己的图像。我尝试实现它,但我需要一些帮助。谁能指导我?这是我的代码:
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
}
}
答案 0 :(得分:1)
通过对代码进行的更改,您需要为我标记的其他类型设置图像名称。
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
}
}
答案 1 :(得分:-1)