如何将两个Web服务数据放入swift 3中的表视图?

时间:2017-09-22 05:34:42

标签: ios uitableview swift3

在这个表视图中我有三个部分,但对于第三部分我使用第二个网址加载数据并传递动态高度并将根据数组计数设置高度这里有时我得到高度有时我得到nill值可以任何人帮我解决这个问题?

此代码如下所示

self.shippingmethodURL()
self.shippingaddressURL()

func shippingmethodURL() {
        guard let url = URL(string: self.shippingMethodURl) else {return}
        URLSession.shared.dataTask(with: url, completionHandler: {(data, response, error) -> Void in
            if let data = data, let jsonObj = (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)) as? [String:Any] {
                self.arrayss = jsonObj as [String : AnyObject]
                for value in jsonObj.values {
                    if let array = value as? [[String:Any]] {
                        for element in array {
                            if (element["name"] as? String) != nil {
                                self.totalCount += 1
                            }
                        }
                    }
                }
                DispatchQueue.main.async { 
                    let sectionHeight = self.arrayss.count * 31
                    let cellHeight = self.totalCount * 44
                    self.heightStart = sectionHeight + cellHeight
                }
            }
        }).resume()
    }
    func shippingaddressURL() {
        let url = NSURL(string: self.url)
        URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
            if let data = data, let jsonObj = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary {
            self.shippingArray = (jsonObj!.value(forKey: "address") as? [[String: AnyObject]])!
                let key = "default"
                for obj in self.shippingArray {
                    if let dict = obj[key] {
                        self.defaultArray.append(dict as! Int)
                    }
                }
                if self.defaultArray.contains(1) {
                    self.addressSelected = true
                    self.selected = false
                } else {
                    //item could not be found
                }
                OperationQueue.main.addOperation({
                    self.tableDetails.reloadData()
                })
            }
        }).resume()
    }

 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if ((addressSelected == true || checkIsPaymentRadioSelect == true) && selected == false){
                if (indexPath.section == 0) {
                    return UITableViewAutomaticDimension
                }
                else if (indexPath.section == 1) {
                    return 62
                }
                else {
                    if height == 0 {
                        if heightStart == nil {
                            shippingmethodURL()
                        }
                        return CGFloat(heightStart!)
                    }
                    else{
                        return CGFloat(self.height)
                    }
                }
            }
            else{
                if (indexPath.section == 0){
                    return UITableViewAutomaticDimension
                }
                else if (indexPath.section == 1){
                    return 62
                }
                else {
                    return 0
                }
            }
    }

0 个答案:

没有答案