如何在单表视图上实现json数据?

时间:2017-08-19 05:36:30

标签: ios uitableview swift3

我在表格视图中有这样的布局。目前,我已根据我的知识在两个表视图中实现了这一点。任何人都可以帮我如何在单表视图上实现它吗?图像应显示为here

第一个表格视图

---送货地址标题

- 地址标签单选按钮(动态)

- 按钮

---运输方式标题标签

第二个表格视图

- 送货方式标题(章节标题)(动态)

- 下面的运输方法(动态)

- 按钮  并且代码是

let url = "http://www.json-generator.com/api/json/get/bMlqRPbjGW?indent=2"
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()
                    let height = (self.shippingArray.count * Int(self.tableHeightConstraintConstant()))
                    self.heightConstraint.constant = CGFloat(height)
//                    self.scrollView.contentSize = CGSize(width: self.scrollView.contentSize.width, height: (CGFloat(height) + 28 + 28))
                })
            }
        }).resume()
    }
    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 {
                self.arrayss = jsonObj as! [String : AnyObject]
                print(self.arrayss)
                self.keys = jsonObj?.allKeys as! [String]
                print(self.keys)
                OperationQueue.main.addOperation({
                    self.shippingTableView.reloadData()
                    let height = self.arrayss.count * 115
                    self.shippingHeightConstraint.constant = CGFloat(height)
                    self.scrollView.contentSize = CGSize(width: self.scrollView.contentSize.width, height: CGFloat(self.heightConstraint.constant + 56) + CGFloat(height))
                })
            }
        }).resume()
    }

func numberOfSections(in tableView: UITableView) -> Int{
        _ = 0
        if tableView.tag == 100{
            return 1
        }
        else if tableView.tag == 110 {
            return arrayss.count
        }
        return dataArray.count
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if tableView.tag == 100{
            return "SHIPPING ADDRESS"
        }
        else  {
            return self.keys[section]
        }
    }
    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
        if tableView.tag == 100 {
            let header = view as! UITableViewHeaderFooterView
            header.textLabel?.textColor = UIColor.gray
            header.textLabel?.textAlignment = NSTextAlignment.center
            header.textLabel?.font = UIFont(name: "Futura", size: 17)
        }
        else {
            let header = view as! UITableViewHeaderFooterView
            header.tintColor = UIColor.white
            header.textLabel?.textColor = UIColor.darkGray
            header.textLabel?.textAlignment = NSTextAlignment.left
            header.textLabel?.font = UIFont(name: "Futura", size: 17)
        }
    }
    @IBAction func newAddressAction(_ sender: Any) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let addtoCartVC = storyboard.instantiateViewController(withIdentifier: "newAddress") as! NewAddressViewController
        self.navigationController?.pushViewController(addtoCartVC, animated: true)
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        if tableView.tag == 100 {
            return shippingArray.count
        }
        else {
            let key = self.keys[section]
            let a :[Any]  = arrayss[key] as! [Any]
            return a.count
        }
    }
    @IBAction func continueButtonAction(_ sender: Any) {
        if selected == false{
            let radiobutton = SCLAlertView()
            _ =  radiobutton.showError("Warning", subTitle: "Please select shipping method", closeButtonTitle: "OK")

        }else{
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let addtoCartVC = storyboard.instantiateViewController(withIdentifier: "payment") as! PaymentMethodViewController
            self.navigationController?.pushViewController(addtoCartVC, animated: true)
        }
    }
    func tableheightConstraint() -> CGFloat {
        switch(self.screenHeight()) {
        case 568:
            return 40

        case 667:
            return 55

        case 736:
            return 55

        default:
            return 60
        }
    }
    @IBAction func deleteAction(_ sender: UIButton) {
        shippingArray.remove(at:sender.tag)
        print(self.shippingArray.count)
        heightConstraint.constant = (CGFloat(self.shippingArray.count * Int(self.tableHeightConstraintConstant()) + Int(tableheightConstraint())))
        if(tableView(tableDetails, numberOfRowsInSection: 0) == 0){
            heightConstraint.constant = (CGFloat(self.shippingArray.count * Int(self.tableHeightConstraintConstant()) + 70))
            titleLabel.isHidden = true
            shippingTableView.isHidden = true
            scrollView.isScrollEnabled = false
        }
        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 : Any =  arr["number"] as AnyObject
            cell.mobileNumberLabel.text = "\(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){
                shippingTableView.isHidden = false
                tableDetails.isScrollEnabled = false
                scrollView.isScrollEnabled = true
                titleLabel.isHidden = false
                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 key = self.keys[indexPath.section]
            var a :[Any]  = arrayss[key] as! [Any]
            var dictionary = a[indexPath.row] as! [String:Any]
            let name = dictionary["name"]
            let price = dictionary ["price"]
            cell.methodNameLabel.text = name  as? String
            cell.priceLabel.text = price as? String
            if chekIndex == indexPath {
                cell.radioButton.isSelected = true
            } else {
                cell.radioButton.isSelected = false
            }
            return cell
        }
    }

0 个答案:

没有答案