如何在 dequeueReusable 单元格中显示适当的图像?

How do I show an appropriate image in dequeueReusable cell?

我有一个显示为 dequeueReusableCell 的数组,我想在单元格旁边显示适当的图像。我的图像与位于资产中的数组中的项目同名。

如何在 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
    }

}

如果您有自定义 table 单元格 class 投射这个

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! MyCellClass
cell.imageCellNicosia?.image = UIImage(named: city)

如果不使用 UITableViewCell

的 imageView 属性 中的构建
cell.imageView?.image = UIImage(named: city)