0

I am trying to save image names in cellImageName in CitySelectViewController (shown as a comment above the line with the error).

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]
        }

How do I get rid of these errors?

        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]
  }
6
  • which error you have face? Commented Dec 13, 2016 at 6:59
  • Ambiguous reference to member 'subscript'.I commented that error in question for your reference Commented Dec 13, 2016 at 6:59
  • Why are you still using dictionaries instead of arrays of City struct as I showed in the last answer. A dictionary is the wrong data structure for a collection view. If you use arrays, you simply need to create another array that contains these arrays and use that to provide sections in your collection view. Commented Dec 13, 2016 at 7:01
  • @Paulw11, I used structs and it worked well for a single array as you showed me , but I was not able to use for multiple arrays as I was not able to figure it out, so I just posted this and I am still working on it. Commented Dec 13, 2016 at 7:05
  • cell.imageView?.image = self.selectedImages[(indexPath as NSIndexPath).item] selectedImages is a dictionary. You shouldn't try to get values like selectedImages[0] and selectedImages dictionary is empty now. Commented Dec 13, 2016 at 7:20

2 Answers 2

1

Based on the code in my answer to your previous question

You can simply create an array of arrays and put each array into its own section in the collection view:

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

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
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Appreciate your understanding of situation and response.
0

first of all your selectedImage is never filled (or at least in the code you provide) so its always an empty dictionary. secondly why you downcast IndexPath to NSIndexpath in cellForItemAt?

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

also your dictionary is type of [String : UIImage] and you're treating it as [Int : UIImage].

also when you are going to use indexpath you can use array instead of [Int, UIImage].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.