我有一个集合视图和 Json 数组。我想通过点击集合视图单元格来获取另一个视图的特定数据。我曾经通过在主视图上的按钮上使用标签来获取数据,但是当我用集合视图更改视图时,现在没有单独的按钮,所以集合视图现在可以使用索引。如何做一种解析器从集合视图中获取带有点击索引的数据?
import Foundation
class CardGenerator {
static func generateCards(onMode mode: Letter) -> JsonEnum {
let filepath = Bundle.main.path(forResource: "Data", ofType: "json")!
let data = NSData(contentsOfFile: filepath)!
let json = try! JSONSerialization.jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions.allowFragments) as! [String: AnyObject]
print(json)
print(json as? NSObject )
let cards: JsonEnum = JsonEnum(img: "", txt: "", lett: "", sound: "")
if let jsonCards = json[mode.rawValue] as? [String: AnyObject] {
//for aso in jsonCards {
print(jsonCards)
let card = JsonEnum()
if let image = jsonCards["image"] as? String {
card.image = image
}
if let text = jsonCards["text"] as? String {
card.text = text
}
if let letter = jsonCards["letter"] as? String {
card.letter = letter
}
if let sound = jsonCards["sound"] as? String {
card.sound = sound
}
print(card.image, card.text, card.letter, card.sound)
return card
}
return cards
}
}