我有一个集合视图和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
}
}
当你选择UICollectionViewCell时,下面的UICollectionViewDelegate会被调用
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
要使用特定单元格选项卡的数据,请使用以下代码:-
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
//here get data from cell
}