2017-10-11 3 views
-2

J'ai la clé du curriculum qui a de nouveau un dictionnaire et à nouveau avoir tableau. Comment le gérer dans tableview avec le nom de la clé de la section sera le titre de l'en-tête . Aidez-moi, s'il vous plaît. Merci d'avance.Comment gérer tableau de tableau de dictionnaire dans swift

curriculam =   { 
     pdf =    (
          { 
       id = 1; 
       link = "google.com"; 
      }, 
          { 
       id = 2; 
       link = "google.com"; 
      } 
     ); 
     ppt =    (
          { 
       id = 1; 
       link = "google.com"; 
      }, 
          { 
       id = 2; 
       link = "google.com"; 
      } 
     ); 
     test =    (
          { 
       id = 1; 
       link = "google.com"; 
      }, 
          { 
       id = 2; 
       link = "google.com"; 
      } 
     ); 
     title = "lesson one"; 
     video =    (
          { 
       id = 1; 
       link = "google.com"; 
      }, 
          { 
       id = 2; 
       link = "google.com"; 
      } 
     ); 
    }; 

Répondre

0

Vous pouvez utiliser comme ceci:

import UIKit 

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    @IBOutlet weak var tableview: UITableView! 
    let curriculam = [ 
     "pdf": [["id":1,"link":"google1.com"], ["id":2,"link":"google2.com"], ["id":3,"link":"google3.com"]], 
     "ppt": [["id":1,"link":"google1.com"], ["id":2,"link":"google2.com"]], 
     "test": [["id":1,"link":"google1.com"], ["id":2,"link":"google2.com"]], 
     "video": [["id":1,"link":"google1.com"], ["id":2,"link":"google2.com"]] 
    ] 

    var sections: [String] = [] 
    var items: [[[String: Any]]] = [[[:]]] 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return sections.count 
    } 

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     return sections[section] 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return items[section].count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell")! 
     var label: String = "" 

     if let id = items[indexPath.section][indexPath.row]["id"] { 
      label = "\(id)" 
     } 
     if let link = items[indexPath.section][indexPath.row]["link"] { 
      label = "\(label)-\(link)" 
     } 

     cell.textLabel?.text = label 
     return cell 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     tableview.delegate = self 
     tableview.dataSource = self 

     sections = curriculam.map { $0.key } 
     items = curriculam.map { $0.value } 
    } 
}