funcGET(pass:@escaping(Any,Bool)->Void){
let urlstr = URL.init(string: "http://api.jisuapi.com/illegal/carorg2?appkey=de394933e1a3e2db")
// 将字符串转换为URL
// let mURL = URL.init(string: urlstr)
// 将URL封装为Request对象
letreq =URLRequest(url: urlstr!, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval:10.0)
// 网络会话对象,请求网络数据
URLSession.shared.dataTask(with: req) { (data:Data?, response:URLResponse?, error:Error?)in
// JSON解析
letjsonData =try?JSONSerialization.jsonObject(with: data!, options: .allowFragments)
letjsonDic = jsonDataas!NSDictionary
letstatus = jsonDic.value(forKey:"status")as!NSString
letmsg = jsonDic.value(forKey:"msg")as!String
ifstatus.intValue!=0{
DispatchQueue.main.async{
print(msg)
}
return
}
letresultDic = jsonDic.value(forKey:"result")as!NSDictionary
letdataArr = resultDic.value(forKey:"data")as!NSArray
varmodelArr:[hqjgj] = []
foritemindataArr{
letitemDic = itemas!NSDictionary
letone =hqjgj()
one.province= (itemDic.value(forKey:"province")as?String)!
modelArr.append(one)
}
pass(modelArr,true)
// pass(jsonData!,true)
// print(jsonData!)
}.resume()
}
================================一级======================
varsearchBtn:UIButton? // 搜索按钮
self.view.backgroundColor = UIColor.white
searchBtn=UIButton(frame:CGRect(x:0, y:0, width:100, height:50))
searchBtn?.center=CGPoint(x:200, y:300)
searchBtn?.setTitle("点击查询", for: .normal)
searchBtn?.backgroundColor = UIColor.black
searchBtn?.setTitleColor(UIColor.white, for: .normal)
searchBtn?.addTarget(self, action:#selector(btnDidPress(sender:)), for: .touchUpInside)
self.view.addSubview(searchBtn!)
}
@objcfuncbtnDidPress(sender:UIButton) ->Void{
lethqVC =HqViewController()
self.present(hqVC, animated:true, completion:nil)
print("进入了第二个控制器")
}
===================二级=========================
vartableDataDic:[String:[String]]? // 用于给表格赋值的字典
vartable:UITableView? // 表格视图
vartableData:[hqjgj]?
self.table=UITableView(frame:self.view.frame, style: .plain)
self.table?.dataSource=self;
self.table?.delegate=self;
self.view.addSubview(self.table!)
}
overridefuncviewWillAppear(_animated:Bool) {
leturlh =URLL()
urlh.GET{ (data, success)in
if!success {
return
}
self.tableData= (dataas? [hqjgj])!
DispatchQueue.main.async {
self.table?.reloadData()
}
print(data)
}
}
functableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int{
ifletcount =tableData?.count{
returncount
}
return0
}
functableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell{
letcellIdentifier ="cell"
varcell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
ifcell ==nil{
cell =UITableViewCell(style: .subtitle, reuseIdentifier: cellIdentifier)
}
letone =self.tableData![indexPath.row]
cell?.textLabel?.text= one.province
returncell!
}
=======================model===============
classhqjgj:NSObject{
varprovince =""
}
网友评论