2016-12-13 6 views
0

cellImageNameに保存しようとしています。これは、エラーの行の上にコメントとして示されています。CitySelectViewControllerにあります。複数の配列のイメージ名を保存する方法は?

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")!] 

let townImages: [String : UIImage] = [ "Town00" : UIImage(named: "town_00")! , "Town01" :UIImage(named: "town_01")!, "Town02" : UIImage(named: "town_02")!] 

let cityBImages: [String : UIImage] = [ "CityB00" : UIImage(named: "city_B00")! , "CityB01" :UIImage(named: "city_B01")!, "CityB02" : UIImage(named: "city_B02")!] 

let townBImages: [String : UIImage] = [ "TownB00" : UIImage(named: "town_B00")! , "TownB01" :UIImage(named: "town_B01")!, "TownB02" : UIImage(named: "town_B02")!] 

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 


     if (indexPath.row == 2) 
     { 
      if self.areas == city 
      { 
       CityVC.imageSelected = cityBImages 
      } 
      else 
       if self.areas == town 
       { 
        CityVC.imageSelected = townBImages 
      } 
      else 
      // Ambiguous reference to member 'subscript' 
      CityVC.imageSelected = self.selectedImages[(indexPath as NSIndexPath).item] 
     } 

これらのエラーを取り除くにはどうすればよいですか?すべてのselectedImageの最初の

 This is `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

? –

+0

メンバー 'subscript'へのあいまいな参照です。私はあなたの参照のために問題のエラーをコメントしました – leaner122

+0

最後の答えで示したように、なぜあなたはまだ 'City'構造体の配列の代わりに辞書を使用していますか?ディクショナリは、コレクションビューの間違ったデータ構造です。配列を使用する場合は、これらの配列を含む別の配列を作成し、それを使用してコレクションビューにセクションを提供するだけです。 – Paulw11

答えて

1

あなたprevious question

に私の答えのコードに基づいて、あなたは、単に配列の配列を作成することができますし、コレクションビューに独自のセクションに各配列を置く:

のstruct市{ VAR名:文字列 するvar imagenameの:文字列 }あなたが顔を持っているエラー

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")] 

let towns = [City(name:"Town00", imageName:"town_00"), 
       City(name:"Town01", imageName:"town_01"), 
       City(name:"Town02", imageName:"town_02")] 

let villages = [City(name:"Village00", imageName:"village_00"), 
       City(name:"Village01", imageName:"village_01"), 
       City(name:"Village02", imageName:"village_02")] 

let allPlaces = [cities, towns, villages] 

func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return self.allPlaces.count 
} 

func collectionView(_ collectionView: UICollectionView, 
numberOfItemsInSection section: Int) -> Int { 
    let places = self.allPlaces[section] 
    return places.count 
} 

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

    let places = self.allCities[indexPath.section] 

    let city = places[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 { 
        let places = self.allPlaces[indexPath.section] 
        selectedCities.append(places[indexPath.item]) 
       } 
       cityVC.selectedCities = selectedCities 
      } 
     } 
    } 
} 
+0

あなたの状況と反応を理解していただければ幸いです。 – leaner122

0

が満たされていない(または少なくともあなたが提供するコードで)ので、その常に空の辞書ありません。第2に、なぜcellForItemAt内のIndexPathをNSIndexpathにダウンキャストするのですか?あなたは[のInt:UIImage]としてそれを治療している:[UIImage文字列]

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

も、あなたの辞書はのタイプです。

indexpathを使用する場合は、[Int、UIImage]の代わりにarrayを使用することもできます。

関連する問題