我有一个数组,我将其显示为 dequeueReusableCell,我想在单元格旁边显示适当的图像。我的图像与位于 Assets 中的数组中的项目同名。
如何在 dequeueReusable 单元格中显示适当的图像?
我的代码:
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)
关于swift - 如何在 dequeueReusablecell 中显示适当的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51225162/