Ios 我想通过点击带有Json解析的集合视图单元格来获取特定数据
我有一个集合视图和Json数组。我想通过点击“集合视图”单元格获取另一个视图上的特定数据。我以前通过在主视图的按钮上使用标记来获取数据,但当我用集合视图更改视图时,现在没有单独的按钮,所以集合视图现在可以使用索引。如何使用一种解析器从集合视图中获取带有抽头索引的数据Ios 我想通过点击带有Json解析的集合视图单元格来获取特定数据,ios,json,swift,uicollectionview,indexpath,Ios,Json,Swift,Uicollectionview,Indexpath,我有一个集合视图和Json数组。我想通过点击“集合视图”单元格获取另一个视图上的特定数据。我以前通过在主视图的按钮上使用标记来获取数据,但当我用集合视图更改视图时,现在没有单独的按钮,所以集合视图现在可以使用索引。如何使用一种解析器从集合视图中获取带有抽头索引的数据 import Foundation class CardGenerator { static func generateCards(onMode mode: Letter) -> JsonEnum {
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
}
选择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
}
如何使用json获取此函数中的数据?您需要做的是在
viewdiload
中调用您的generateCards
方法并在变量中获取输出,然后在collectionView的didSelectItemAt
方法中使用indexPath.row
从generateCards
中获取输出变量索引处的值使用json获取此函数中的数据?您需要做的是在viewdiload
中调用generateCards
方法并在变量中获取输出,然后在collectionView的didSelectItemAt
方法中使用indexPath.row从generateCards
获取输出变量索引处的值