gpt4 book ai didi

ios - 从字典中调用图像时出错

转载 作者:行者123 更新时间:2023-11-28 12:34:59 25 4
gpt4 key购买 nike

我正在尝试创建图像字典,其名称如下。我收到以下错误(显示为错误行上方的注释)。

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

如何消除这些错误?

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

最佳答案

字典与 UICollectionViewUITableView 不太匹配,因为它们是无序的;这意味着您不能使用整数索引进行索引(除非您使用 Int 作为键,但这实际上是一个数组)并且如果您枚举键或值,则返回这些键或值的顺序未定义。

您添加的第二个 View Controller 澄清了您真正的问题;你想向第二个 View Controller 发送多条信息。我仍然不完全清楚第二个 View Controller 的目的是什么,并且由于您使用的是另一个 Collection View ,我假设您希望能够通过多个选定的城市,尽管您的 segue 似乎是在单个选择上触发的,所以这暂时行不通。我已经展示了通过多项选择的代码,无论如何

您应该创建一个struct 来保存您的城市数据并使用该结构的数组:

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

关于ios - 从字典中调用图像时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41107416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com