我有一個結構來存儲它們在應用已經捆綁畫面的名稱和圖像名稱如下如何用具有相應圖像名稱的結構替換字符串名稱?
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
}
顯示城市形象,我得到以下輸出爲例外
City00
City02
,但我不知道如何在tableview中的細胞替代City00 with city_00
和City02 with city_02
,使其顯示圖像。
這可以幫助你:http://stackoverflow.com/questions/25665122/how-to-search-an-array-containing-struct-elements-in-swift –
@JimmyJames,這個問題是爲了搜索,我正在尋找替換 – leaner122
檢查添加或追加字符串到nowCityImages的語句。它應該類似於city.imageName而不是city.name。例如nowCityImages.append(cities [0] .imageName) – bunty