带有侧边菜单的xcode 9 webview

时间:2017-11-04 19:05:18

标签: xcode webview swift4

我需要一个带有表视图的侧面菜单,在其中我需要从json文件中获取带有链接的名称,在菜单中它必须显示来自json文件的名称,当我点击名称时,我必须在我的Web视图中打开URL。 Alsow我需要代替json文件,json url例子,请帮帮我

这是我项目的链接 https://ufile.io/7qpzq

1 个答案:

答案 0 :(得分:0)

导入UIKit

class SideMenuVC:UITableViewController {     最后让urlString =“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
}