我正在制作一个显示口袋妖怪及其类型的应用程序。该应用程序的一部分也会显示出它们的弱点。
我有一个列出所有神奇宝贝的全局变量,如下所示:
var objects = [
{
"id": "001",
"typeTwo": "Poison",
"name": "Bulbasaur",
"type": "Grass"
},
{
"id": "002",
"typeTwo": "Poison",
"name": "Ivysaur",
"type": "Grass"
},
{
"id": "025",
"typeTwo": "",
"name": "Pikachu",
"type": "Electric"
}]
etc...
当用户从第一个 VC 中选择一个 Pokemon 时,他们按下一个按钮,然后将该 Pokemon 的对象传递给我的CollectionViewController
- 并将其存储在var selectedPokemonObject = [String:String]()
我有我的CollectionViewController
设置为过滤我的 Pokemon 对象并仅显示与用户选择的 Pokemon 类型相匹配的 Pokemon。例如:
用户选择:Pikachu (type "Electric"
)
然后用户转到CollectionViewController
以及所有类型的神奇宝贝"Electric"
显示。我的一些代码是:
var filteredObjects = [[String:String]]()
override func viewDidLoad() {
super.viewDidLoad()
filteredObjects = objects.filter{
let valueType = $0["type"]
let valueTypeTwo = $0["typeTwo"]
if (valueType == selectedPokemonObject["type"] || valueType == selectedPokemonObject["typeTwo"]) && (valueTypeTwo == selectedPokemonObject["typeTwo"] || valueTypeTwo == selectedPokemonObject["type"])
{
return true
}
return false
}
}
然后我在中设置单元格信息cellForItemAtIndexPath
如下:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell
let object = filteredObjects[indexPath.row]
cell.pokemonName.text = object["name"]!
cell.pokemonImage.image = UIImage(named: "\(object["id"]!)"+"\(object["name"]!)"+"_small")
return cell
}
所有这些都有效。
我希望发生什么
而不是CollectionViewController
显示与用户选择的类型相同的神奇宝贝,我希望它显示具有我定义的类型的神奇宝贝。例如:
用户选择:Pikachu ("type": "Electric"
)
然后用户转到CollectionViewController
以及所有类型的神奇宝贝"Ground"
显示。或者,更复杂的情况:
用户选择:妙蛙种子 ("type": "Grass"
and "typeTwo": "Poison"
)
然后用户转到CollectionViewController
以及所有类型的神奇宝贝"Fire"
, "Ice"
, "Flying"
and "Psychic"
显示。
我真的很难弄清楚这一点,因此非常感谢您的帮助。我查看了 Switch 语句,但不确定这是否是正确的方法?谢谢