【发布时间】:2016-12-23 17:34:34
【问题描述】:
我有一个结构来存储已经捆绑在应用程序中的图片的名称和图像名称,如下所示
struct City { var name: String
var imageName: String }
class firstViewController: UIViewController
let cities = [City(name:"City00", imageName:"city_00"),
City(name:"City01", imageName:"city_01"),
City(name:"City02", imageName:"city_02")]
在 displayViewController 中,我有一个变量 nowCityImages 来存储从 firstViewcontroller 中选择的数据
class displayViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {
var nowCityImages: [String] = []
现在我想在 tableview 中显示城市的图像
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let myCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CityCell
let outCityImages = nowCityImages[indexPath.item]
// This is not displaying any images
myCell.cityImage.image = UIImage(named:outCityImages)
return myCell
}
如果我使用print(outCityImages),我会得到以下输出作为例外
City00
City02
但我不知道如何在 tableview 单元格中替换 City00 with city_00 和 City02 with city_02 以便显示图像。
【问题讨论】:
-
@JimmyJames,这个问题是用来搜索的,我正在寻找替代
-
检查您向 nowCityImages 添加或附加字符串的语句。它应该类似于 city.imageName 而不是 city.name。例如nowCityImages.append(cities[0].imageName)
标签: swift uitableview struct