2017-07-31 2 views
0

私はこのようなJSONファイルJsonファイルを2回のコレクションビューで素早く解析するには?

{ 
    "dicts": [ 
    { 
     "main": "", 
     "note1": "", 
     "note2": "", 
     "note3": "", 
     "note4": "" 
    }, 
    { 
     "main": "", 
     "note1": "", 
     "note2": "", 
     "note3": "", 
     "note4": "", 
     "note5": "" 
    }, 
    { 
     "main": "", 
     "note1": "", 
     "note2": "" 
    } 
    ] 
} 

私のアプリは2 view controllerscollection viewを持つ各を持っています。最初にcollection viewmainのそれぞれのを表示します。mainをクリックすると、それぞれの内容のnoteのデータをcellに設定する際に問題が発生します。私は誰もがnoteという値を呼ぶことができました。

モデル:私はname.textに一つだけのデータを入力することができる細胞で更新する方法

struct SecPage { 
    var note1:String? 
    var note1:String? 
    var note2:String? 
    var note3:String? 
    var note4:String? 
    var note5:String? 
} 

static func downSec() -> [SecPage] { 
    let jsonFile = Bundle.main.url(forResource: "info", withExtension: "json") 
    let jsonData = try? Data(contentsOf: jsonFile!) 
    var noteArray = [SecPage]() 

    do{ 
     if let jsonResult = try JSONSerialization.jsonObject(with: jsonData!, options: .mutableContainers) as? Dictionary<String,AnyObject>{ 
      let name = jsonResult["dicts"] as? [Dictionary<String,AnyObject>] 
      for note in name!{ 
       let note1 = note["note1"] as? String 
       let note2 = note["note2"] as? String 
       let note3 = note["note3"] as? String 
       let note4 = note["note4"] as? String 
       let note5 = note["note5"] as? String 
       let noteinfo = SecPage(note1: note1 ,note2: note2,note3: note3, note4: note4, note5: note5, note6: note6) 
       noteArray.append(noteinfo) }} } catch{print("note not found")} 
    return noteArray 
} 

ビュー

class CollectionViewCell2: UICollectionViewCell { 
    @IBOutlet weak var img: UIImageView! 
    @IBOutlet weak var names: UILabel! 

    func updateUIsec(data:dataObj) { 
     names.text = data.note1 
    } 
} 

とどのように効率的にjsonを取得するためにModel classを使用する方法ファイルを開き、viewに送信してください。

+1

あなたはJSONを変更できますか?あまり役に立ちません。 'note1'や' note2'などの代わりに、文字列の配列である 'notes'が必要です。 – Paulw11

+0

'notes 'の配列を持っている場合、' cell label'を更新するには? – Digs

+0

さて、 'main''String'プロパティと' notes'' [String ]'プロパティを持つ 'SecPage'構造体があります。 JSONをこれらの構造体の配列にパースします。その後、配列から適切な要素を2番目のView Controllerに渡します。 – Paulw11

答えて

1

これがあなたが探しているものであるかどうかはまだ分かりません。あなたの質問は広すぎて理解しづらいものです。答えをコメントし、必要に応じて、私はそれを更新します;)

VC1:

class VC1: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 
    @IBOutlet weak var collectionView: UICollectionView! 
    var modelData = [SecPage]() 
    var selectedItem: SecPage? 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     collectionView.delegate = self 
     collectionView.dataSource = self 

     //collectionView.register... // Register UICollectionView cell subclass 
     modelData = SecPage.downSec() 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     let secPage = modelData[indexPath.row] 
     let cell = collectionView.dequeue... 
     cell.textLabel.text = secPage.main 

     return cell 
    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

     if let destination = segue.destination as? VC2{ 
      destination.selectedItem = selectedItem 
     } 
    } 

    override func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) { 

     selectedItem = modelData[indexPath.row] 
     performSegue... 
    } 
} 

VC2:

class VC2: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 
    @IBOutlet weak var collectionView: UICollectionView! 
    var selectedItem: SecPage? = nil 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     collectionView.delegate = self 
     collectionView.dataSource = self 

     //collectionView.register... // Register UICollectionView cell subclass 
     modelData = SecPage.downSec() 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeue... 
     cell.updateUIsec(data: selectedItem!) 

     return cell 
    } 
} 

セル:

class CollectionViewCell2: UICollectionViewCell { 
    @IBOutlet weak var img: UIImageView! 
    @IBOutlet weak var names: UILabel! 

    func updateUIsec(data: SecPage) { 
     names.text = data.note1 
    } 
} 
+0

ありがとうございました....全体の問題は、あなたが示したように、 'var selectedItem:SecPage!'を実行するのではなく、2番目のVCで配列のインスタンス 'var selectedItem = [SecPage]()' – Digs

関連する問題