Я пытаюсь сохранять имена изображений в 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]
}
Как избавиться от этих ошибок?
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]
}
который ошибка у вас есть лицо? –
Неоднозначная ссылка на членский индекс. Я прокомментировал эту ошибку для вашей ссылки. – leaner122
Почему вы все еще используете словари вместо массивов структуры City, как я показал в последнем ответе. Словарь - неправильная структура данных для представления коллекции. Если вы используете массивы, вам просто нужно создать другой массив, содержащий эти массивы, и использовать его для предоставления разделов в вашем представлении коллекции. – Paulw11