第三方![只导入工程](https://i-blog.csdnimg.cn/blog_migrate/7ac6a19a77813ab5c4b00c7792e8d59d.png)
import Alamofire
import UIKit
class FourVC: UIViewController ,UITableViewDelegate,UITableViewDataSource{
var tableView = UITableView()
var dic:NSDictionary?
var dictr:NSDictionary = [:]
var dic1:NSArray = []
var imgUrl:String = ""
var dicLocal:NSDictionary = [:]
override func viewDidLoad() {
super.viewDidLoad()
tableView = UITableView.init(frame: self.view.frame, style: .plain)
tableView.delegate=self
tableView.dataSource=self
tableView.register(TableViewCell.self, forCellReuseIdentifier: "cell")
self.view.addSubview(tableView)
// Do any additional setup after loading the view.
Alamofire.request("http://live.ximalaya.com/live-web/v4/homepage?device=iPhone", method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
if(response.error == nil){
print("请求成功")
print(response.result.value as Any)
self.dic = response.result.value as? NSDictionary
self.dictr = self.dic?.object(forKey: "data") as! NSDictionary
self.dic1 = self.dictr.object(forKey: "localRadios") as! NSArray
for i in 0...2{
self.dicLocal = self.dic1[i] as! NSDictionary
self.imgUrl = self.dicLocal.object(forKey: "coverLarge") as!String
}
print(self.imgUrl);
}else{
print("请求失败\(String(describing: response.error))")
}
//解析网址
self.tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dic1.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let name:NSDictionary = dic1[indexPath.row] as! NSDictionary
let cell :TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
cell.textLabel?.text=name["name"] as? String
if let url = NSURL(string: self.imgUrl) {
if let data = NSData(contentsOf: url as URL){
cell.imageView?.image=UIImage(data: data as Data)
// self.imgV.image = img
}
}
cell.accessoryType = UITableViewCell.AccessoryType(rawValue: 1)!
return cell;
}
}