1

我有一个集合视图和 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
    }
}
4

1 回答 1

0

当您选择 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
        }
于 2016-11-01T09:01:57.083 回答