Ios 如何将此web服务数据传递到表视图?
我有两个阵列的web服务数据,我需要将其传递到表视图,如下所示: 传递的数据将如下面的url所示,我正在尝试将数组名作为节实现到另一个表视图中,只有我使用内部字典将其传递到原型单元格中Ios 如何将此web服务数据传递到表视图?,ios,xcode,uitableview,swift3,Ios,Xcode,Uitableview,Swift3,我有两个阵列的web服务数据,我需要将其传递到表视图,如下所示: 传递的数据将如下面的url所示,我正在尝试将数组名作为节实现到另一个表视图中,只有我使用内部字典将其传递到原型单元格中 let urlString = "http://www.json-generator.com/api/json/get/bVWKKHtWbm?indent=2" func shippingaddressURL() { let url = NSURL(string: self.url) URL
let urlString = "http://www.json-generator.com/api/json/get/bVWKKHtWbm?indent=2"
func shippingaddressURL() {
let url = NSURL(string: self.url)
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 {
self.shippingArray = (jsonObj!.value(forKey: "address") as? [[String: AnyObject]])!
OperationQueue.main.addOperation({
self.tableDetails.reloadData()
})
}
}).resume()
}
func assignValuesToObjecs(arrayvalues: NSArray){
(arrayvalues as NSArray).enumerateObjects({ (object, count, stop) in
let Object :shippingObject = shippingObject()
Object.name = (object as AnyObject) .value(forKey: "name") as! String
Object.id = (object as AnyObject) .value(forKey: "id") as! NSInteger
Object.price = (object as AnyObject).value(forKey: "price")as! String
self.detailsArray.append(Object)
})
}
func shippingmethodURL() {
let url = NSURL(string: self.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!)
self.arrayss = (jsonObj?.allKeys)! as! [String]
//self.shippingArray = (jsonObj!.value(forKey: "Flat") as? [[String: AnyObject]])!
self.assignValuesToObjecs(arrayvalues :(jsonObj?["Flat"] as? [[String: AnyObject]]! as? NSArray)!)
self.assignValuesToObjecs(arrayvalues :(jsonObj?["United Parcel Service"] as? [[String: AnyObject]]! as? NSArray)!)
self.methodArray = (jsonObj!.value(forKey: "United Parcel Service") as? [[String: AnyObject]])!
OperationQueue.main.addOperation({
self.tableDetails.reloadData()
self.keys = ["Flat","United Parcel Service"]
})
}
}).resume()
}
@IBAction func selectRadioButton(_ sender: KGRadioButton) {
let chekIndex = self.checkIsRadioSelect.index(of: sender.tag)
_ = self.checkIsButtonEnable.index(of: sender.tag)
if sender.isSelected {
} else{
if(chekIndex == nil){
self.checkIsRadioSelect.removeAll(keepingCapacity: false)
self.checkIsRadioSelect.append(sender.tag)
self.checkIsButtonEnable.removeAll(keepingCapacity: false)
self.checkIsButtonEnable.append(sender.tag)
self.tableDetails.reloadData()
}
}
}
func numberOfSections(in tableView: UITableView) -> Int{
if tableView.tag == 100{
return 1
}
else {
return keys.count
}
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if tableView.tag == 100{
return "SHIPPING ADDRESS"
}
else {
return "SHIPPING METHOD"
}
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
if tableView.tag == 100 {
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.darkGray
header.textLabel?.textAlignment = NSTextAlignment.center
header.textLabel?.font = UIFont(name: "Futura", size: 17)
}
else {
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.darkGray
header.textLabel?.textAlignment = NSTextAlignment.center
header.textLabel?.font = UIFont(name: "Futura", size: 17)
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
if tableView == self.tableDetails{
return shippingArray.count
}
else {
return detailsArray.count
}
}
func continueButtonAction(button : UIButton){
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "payment") as! newAddressViewController
self.present(nextViewController, animated:true, completion:nil)
}
func footerButtonAction(button : UIButton){
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "newAddress") as! newAddressViewController
self.present(nextViewController, animated:true, completion:nil)
}
@IBAction func deleteAction(_ sender: UIButton) {
shippingArray.remove(at:sender.tag)
self.tableDetails.reloadData()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView.tag == 100{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! addressTableViewCell
tableDetails.isHidden = false
myActivityIndicator.stopAnimating()
let arr = shippingArray[indexPath.row]
cell.deleteButton.tag = indexPath.row
cell.nameLabel.text = arr["name"] as? String
cell.addressLabel.text = arr["address"]as? String
let mobilenumber : Int = arr["number"] as! Int
cell.mobileNumberLabel.text = String(describing: mobilenumber)
cell.radioButton.tag = indexPath.row
cell.editButton.tag = indexPath.row
cell.deleteButton.tag = indexPath.row
cell.editButton.isHidden = true
cell.deleteButton.isHidden = true
let checkIndex = self.checkIsRadioSelect.index(of: indexPath.row)
if(checkIndex != nil){
cell.radioButton.isSelected = true
cell.editButton.isHidden = false
cell.deleteButton.isHidden = false
}else{
cell.radioButton.isSelected = false
cell.editButton.isHidden = true
cell.deleteButton.isHidden = true
}
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "shippingCell", for: indexPath) as! shippingMethodTableViewCell
let arr = detailsArray[indexPath.row]
cell.methodNameLabel.text = arr.name
cell.priceLabel.text = arr.price
return cell
}
}