2017-11-04 12 views
-4

サイドビューにはテーブルビューが必要ですが、jsonファイルから名前を取得する必要があります。メニューではjsonファイルの名前を表示する必要があります。名前をタップするとURLを開く必要があります私のウェブビューで。 "のUITableViewController { 最終ましょうurlString =:私はJSONファイル、JSONのURLの例の代わりに必要 Alsowは、ここに私を助けてくださいxcode 9サイドビューのwebview

は私のプロジェクト https://ufile.io/7qpzq

答えて

0

輸入のUIKit

クラスSideMenuVCへのリンクですhttp://www.mocky.io/v2/5a02ad1d330000292bf6efa0

var nameArray = [String]() 
    var iconArray = [String]() 
    var URLArray = [String]() 



override func viewDidLoad() { 
    super.viewDidLoad() 

self.downloadJsonWithURL()

} 

func downloadJsonWithURL() { 
    let url = NSURL(string: urlString) 
    URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in 

     if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary { 
      print(jsonObj!.value(forKey: "menuItems") as Any) 

      if let menuArray = jsonObj!.value(forKey: "menuItems") as? NSArray { 
       for menuItems in menuArray{ 
        if let menuDict = menuItems as? NSDictionary { 
         if let name = menuDict.value(forKey: "name") { 
          self.nameArray.append(name as! String) 
         } 
         if let icon = menuDict.value(forKey: "icon") { 
          self.iconArray.append(name as! String) 
         } 
         if let url = menuDict.value(forKey: "url") { 
          self.URLArray.append(name as! String) 
         } 

        } 

       } 

      } 

      OperationQueue.main.addOperation({ 
       self.tableView.reloadData() 
      }) 
     } 
    }).resume() 
} 


func downloadJsonWithTask() { 

    let url = NSURL(string: urlString) 

    var downloadTask = URLRequest(url: (url as URL?)!, cachePolicy: URLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 20) 

    downloadTask.httpMethod = "GET" 

    URLSession.shared.dataTask(with: downloadTask, completionHandler: {(data, response, error) -> Void in 

     let jsonData = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) 

     print(jsonData as Any) 

    }).resume() 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return nameArray.count 
} 
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    _ = nameArray[indexPath.row] 
    tableView.deselectRow(at: indexPath, animated: true) 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "menu", for: indexPath) 
    let label = cell.viewWithTag(1000) as! UILabel 

    label.text = nameArray[indexPath.row] 

    return cell 
} 
関連する問題