2017-07-31 19 views
0

我有這樣如何在swift中解析Json文件進行2次集合視圖?

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

我的應用JSON文件2 view controllers各有collection view。在第一個collection view我將顯示JSON數據main爲每個cell,當我點擊main我有困難設置note數據爲各自main內容在每個cell。所有我能夠打電話給任何一個note價值。

型號:

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 
    } 
} 

如何在細胞更新的時候,我可以能夠在name.text只輸入一個數據,以及如何有效地使用Model class檢索json文件併發送到view

+1

你可以改變你的JSON?這不是非常有用。而不是使用'note1'' note2'等,你應該有'notes'這是一個字符串數組。 – Paulw11

+0

那麼如果我有'筆記'數組,如何更新'單元格標籤'? – Digs

+0

那麼,你會有一個'SecPage'結構,它具有'main''String'屬性和''notes'' [String]'屬性。將您的JSON解析爲這些結構的數組。然後,將適當的元素從數組傳遞給第二個視圖控制器。 – 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

非常感謝你....整個問題是一個愚蠢的事情是在第二個VC而不是做'var selectedItem:SecPage!'如你所示我創建了數組實例'var selectedItem = [SecPage]()' – Digs