我有一个集合视图和Json数组。我想通过点击集合视图单元格获取另一个视图的特定数据。我以前通过在主视图上的按钮上使用标签来获取数据,但是当我使用集合视图更改视图时,现在没有单独的按钮,因此集合视图现在可以使用索引。如何从集合视图中使用tapped索引获取数据?
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
}
}
答案 0 :(得分: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
}