swift - 如何在 dequeueReusablecell 中显示适当的图像?

标签 swift uitableview uiimage

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

相关文章:

Swift 3 如何使用 Vapor 发送多部分发布请求

ios - uipageviewcontroller 中的 Swift uipageviewcontroller

swift - iOS Sprite 接触检测

iphone - iPhone 应用程序的示例源代码,显示 UITableView 的编辑/删除/添加行

ios - 如何将数据放在 UITableViewCell 的右侧

iphone - 在后台加载 MKMapView 并从中创建 UIImage (iPhone 和 iPad)

arrays - 在 swift 3 中将二进制字符串转换为整数数组

swift - 在 TableView 中选择和取消选择行时执行函数的问题

iphone - 如何获取 UIImage 中使用的图像名称?

objective-c - 如何在 Objective C 中使用 NSData 存储图像