Swift 如何在可重用单元中显示适当的图像?
我有一个数组,显示为dequeueReusableCell,我想在单元格旁边显示一个合适的图像。我的图像与资产中数组中的项目同名 如何在可重用单元中显示适当的图像 我的代码:Swift 如何在可重用单元中显示适当的图像?,swift,uitableview,uiimage,Swift,Uitableview,Uiimage,我有一个数组,显示为dequeueReusableCell,我想在单元格旁边显示一个合适的图像。我的图像与资产中数组中的项目同名 如何在可重用单元中显示适当的图像 我的代码: import UIKit class Nicosia: UIViewController, UITableViewDelegate, UITableViewDataSource { let nicosiaPlaces = ["Famagusta Gate", "Laiki Geitonia", "Ledra
import UIKit
class Nicosia: UIViewController, UITableViewDelegate, UITableViewDataSource {
let nicosiaPlaces = ["Famagusta Gate", "Laiki Geitonia", "Ledra Street","Omeriye Hamam","Cyprus Museum","Venetian Walls","The House of Hatjigeorgakis Kornessios","Byzantine Art Museum","Archbishop's Palace","Liberty Monument","The Faneromeni Church","Nicosia International Conference Center"]
var identities = ["Famagusta Gate", "Laiki Geitonia", "Ledra Street","Omeriye Hamam","Cyprus Museum","Venetian Walls","The House of Hatjigeorgakis Kornessios","Byzantine Art Museum","Archbishop's Palace","Liberty Monument","The Faneromeni Church","Nicosia International Conference Center"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return nicosiaPlaces.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
let city = nicosiaPlaces [indexPath.row]
cell?.textLabel?.text = city
//cell.imageCellNicosia?.image = UIImage(named: city)
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
}
如果您有一个自定义的表格单元格类,请将此
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! MyCellClass
cell.imageCellNicosia?.image = UIImage(named: city)
如果没有,请使用UITableViewCell的内置imageView属性
cell.imageView?.image = UIImage(named: city)