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

时间:2018-07-07 16:52:09

标签: swift uitableview uiimage

我有一个数组,显示为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
    }

}

1 个答案:

答案 0 :(得分:0)

如果您有一个自定义表格单元格类,则将其强制转换

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

如果不使用UITableViewCell的imageView内置属性

cell.imageView?.image = UIImage(named: city)