Ios 如何将web服务数据传递到tableview?
我在shipping方法中有如下所示的webservices数据Ios 如何将web服务数据传递到tableview?,ios,uitableview,swift3,Ios,Uitableview,Swift3,我在shipping方法中有如下所示的webservices数据 { "Flat": [ { "price": "$5.00", "id": 11, "name": "Fixed" } ], "United Parcel Service": [ { "price": "$109.12", "id": 1, "name": "worldwide Expedited" }
{
"Flat": [
{
"price": "$5.00",
"id": 11,
"name": "Fixed"
}
],
"United Parcel Service": [
{
"price": "$109.12",
"id": 1,
"name": "worldwide Expedited"
},
{
"price": "$120.18",
"id": 2,
"name": "worldwide Express saver"
}
]
}
我需要如下所示将此数据传递到表视图,有人能帮助我如何在表视图中动态实现此功能吗?这是一个平面和联合包裹服务都是动态的,内部阵列也是动态的。有人能帮助我如何将数据传递到表视图以显示,如图所示,我的代码如下所示
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()
})
}
}).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{
// #warning Incomplete implementation, return the number of sections
return 5
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "SHIPPING ADDRESS"
}
else if section == 2{
return "SHIPPING METHOD"
}
else{
return ""
}
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
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 (section == 0){
return shippingArray.count
}else if (section == 1){
return 1
}else if (section == 2) {
return arrayss.count
}else if (section == 3){
return detailsArray.count
}
else {
return 1
}
}
@IBAction func deleteAction(_ sender: UIButton) {
shippingArray.remove(at:sender.tag)
self.tableDetails.reloadData()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0{
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 if indexPath.section == 1{
let cell = tableView.dequeueReusableCell(withIdentifier: "addresscell", for: indexPath) as! newAddressTableViewCell
return cell
}
else if indexPath.section == 2{
let cell = tableView.dequeueReusableCell(withIdentifier: "shippingcell", for: indexPath) as! shippingMethodTableViewCell
cell.methodLabel.text = arrayss[indexPath.row]
return cell
}
else if indexPath.section == 3{
let cell = tableView.dequeueReusableCell(withIdentifier: "shippingTypeCell", for: indexPath) as! shippingMethodTypeTableViewCell
var Object : shippingObject = shippingObject()
Object = detailsArray[indexPath.row]
cell.nameLabel.text = Object.name
cell.priceLabel.text = Object.price
return cell
}
else{
let cell = tableView.dequeueReusableCell(withIdentifier: "continuecell", for: indexPath) as! continueTableViewCell
return cell
}
}
任何帮助都是非常感谢的只需使用通知后观察者并重新加载表格否我能够显示数据,但不能以适当的方式显示,如下面@Sanjeetverma的装运方法中的图像所示