2016-12-12 3 views
0

名前の画像辞書を次のように作成しようとしています。私はエラーに続いています(エラーのある行の上にコメントとして表示されます)。辞書から画像を呼び出す際のエラー

ViewController.swift

var selectedImages = [String : UIImage ]() 

let cityImages: [String : UIImage] = [ "City00" : UIImage(named: "city_00")! , "City01" :UIImage(named: "city_01")!, "City02" : UIImage(named: "city_02")!] 


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{ 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) 
     as! CollectionViewCell 

    // Ambiguous reference to member 'subscript' 
    cell.imageView?.image = self.selectedImages[(indexPath as NSIndexPath).item] 

    return cell 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "showOptions" 
    { 
     let indexPaths = self.collectionView!.indexPathsForSelectedItems! 
     var indexPath = indexPaths[0] as IndexPath 
     let CityVC = segue.destination as! CitySelectViewController 

     // Ambiguous reference to member 'subscript' 
     CityVC.imageSelected = self.selectedImages[(indexPath as NSIndexPath).item] 
    } 

どのように私は、これらのエラーを取り除くのですか?

これはあなたの辞書はキーとしてUIImageを使用し、文字列値のためにそれをマッピングしCitySelectViewController.swift

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

    return self.imageSelected.count 

} 


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{ 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) 
     as! CityCollectionViewCell 

    cell.imageView?.image = imageSelected[(indexPath as NSIndexPath).row] 

    return cell 
} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let cell = collectionView.cellForItem(at: indexPath) 

    cellImage = imageSelected[(indexPath as NSIndexPath).row] 
    cellImageName = imageSelected[(indexPath as NSIndexPath).row] 
} 
+0

最初のエラーは、 'UIImage'キーを持つ辞書のキーとして整数を使用しているためです。 2番目のものには同じ問題があり、そのまわりにはさらに '[]'があります。 – dan

+0

都市名の配列が必要であると思われますので、文字列の都市名を '[String:UIImage]'の辞書のキーとして使用する必要があります。辞書自体は順序付けされていないため、コレクションビューとの良好な一致はありません。コレクションビューには '.row'ではなく' indexPath.item'を使うべきです – Paulw11

+0

@ Paulw11、あなたが言ったようにしましたが、私はまだこのあいまいなエラーが出ています。私はちょうど私の現在の解決に私の質問を編集しました – leaner122

答えて

2

辞書はUICollectionViewUITableViewと非常に良く一致していないような変更をお勧めしたいです。これは、整数インデックスを使用してインデックスを作成できないことを意味します(Intをキーとして使用しない限り、実際は配列です)。定義されていません。

2番目のビューコントローラを追加すると、実際の質問が明確になります。 2つ以上の情報を2つ目のView Controllerに送信したいとします。私はまだ2番目のビューコントローラの目的が何であるかは完全にはっきりしておらず、別のコレクションビューを使用しているので、選択した複数の都市を渡すことができますが、今は動作しません。

struct City { 
    var name: String 
    var imageName: String 
} 

class firstViewController: UIViewController // Or UICollectionViewController 

let cities = [City(name:"City00", imageName:"city_00"), 
       City(name:"City01", imageName:"city_01"), 
       City(name:"City02", imageName:"city_02")] 


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{ 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) 
    as! CollectionViewCell 

    let city = self.cities[indexPath.item] 

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

    return cell 
} 


override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "showOptions" { 
     if let indexPaths = self.collectionView!.indexPathsForSelectedItems { 
      if let cityVC = segue.destination as? CitySelectViewController { 
       var selectedCities = [City]() 
       for indexPath in indexPaths { 
        selectedCities.append(self.cities[indexPath.item]) 
       } 
       cityVC.selectedCities = selectedCities 
      } 
     } 
    } 
} 

、あなたのCitySelectViewController

class CitySelectViewController { 

    var selectedCities = [City]() 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return self.selectedCities.count 
    } 


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{ 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) 
    as! CityCollectionViewCell 

     let city = self.selectedCities[indexPath.item] 
     cell.imageView?.image = UIImage(named:city.imageName) 

     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     let city = self.selectedCities[indexPath.item] 
     cellImage = UIImage(named: city.imageName) 
     cellName = city.name 
    } 
} 
に:私は関係なく、

あなたはあなたの街のデータを保持し、その構造体の配列を使用するようにstructを作成する必要があります複数選択を渡すコードを示しています

+0

唯一の理由は、コレクションビューで画像を選択したときに画像の名前を保存することでした。 – leaner122

+0

'cellForItemAtIndexPath'に文字列配列を格納し、' UIImage(named:) 'を使うことができます。画像を辞書にプリロードするだけで、より多くのメモリが使用され、時間が節約されない – Paulw11

+0

私は他のviewControllerとその理由を追加しました。辞書を作成していました。私は迅速に新しいです、それに基づいて任意の提案は、あなたが今までに明らかにすべてを理解することができるようになりますように感謝されます。 – leaner122

0

です。あなたはフォーマットCity0xの画像を追加する場合は通常、あなたは、あなたがそう

let imageName = "City0" + String(describing: (indexPath as NSIndexPath).row) 
let image = self.selectedImages[imageName] 

のようなあなたの画像を得ることができます次に、あなたのイメージを持っていて、何でも行うことができ

var selectedImages = [String : UIImage]() 

のように、それを別の方法を行いたいですあなたは欲しい。 しかし、あなたは唯一のindexPathの行を使用してそれらの画像を取得する場合、私は彼らが順序付けられていないよう

var selectedImages = [UIImage]() 

let image = self.selectedImages[(indexPath as NSIndexPath).row] 
関連する問題