如何在按钮激活期间调用api?

时间:2017-09-11 04:29:20

标签: ios uitableview swift3

在这个我在表视图中有三个部分,其中第一部分将有地址和单选按钮,如果我点击单选按钮它将激活,特定地址将发布,具体取决于第三部分需要的地址选择调用api并在第二个表视图中加载数据,第三部分中是否有人可以帮助我如何执行此操作?

这是表视图类的代码

  func numberOfSections(in tableView: UITableView) -> Int
    {
         if ((addressSelected == true || checkIsPaymentRadioSelect == true) && selected == false) {
            return 3
        }else {
            return 2
        }
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
    {
        if ((addressSelected == true || checkIsPaymentRadioSelect == true) && selected == false) {
            if (section == 0) {
                return "SHIPPING ADDRESS"
            }
            else if (section == 2) {
                return "SHIPPING METHOD"
            }
            else  {
                return ""
            }
        }
        else {
                if (section == 0) {
                return "SHIPPING ADDRESS"
                }
                else{
                    return ""
                }
        }
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        if (section == 0)
        {
            return shippingArray.count
        }
        else
        {
            return 1
        }
    }
    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 {
                print(height)
                return CGFloat(height)
            }

        }
        else{
            if (indexPath.section == 0){
                return UITableViewAutomaticDimension
            }
            else if (indexPath.section == 1){
                return 62
            }
            else {
                return 0
            }

        }
    }


  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
        activityIndicator.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
        cell.radioButton.addTarget(self, action: #selector(selectRadioButton(_:)), for: .touchUpInside)
        cell.deleteButton.addTarget(self, action: #selector(deleteAction(button:)), for: .touchUpInside)
        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
        }
        if (checkIsPaymentRadioSelect == true){
        let defaultvalue  = arr["default"] as! Int
        if defaultvalue == 1 {
            cell.radioButton.isSelected = true
            cell.editButton.isHidden = false
            cell.deleteButton.isHidden = false
            addressSelected = true
            tableDetails.tableFooterView?.isHidden = false
        }
        }
        return cell
    }
    else if (indexPath.section == 1){
        let cell = tableView.dequeueReusableCell(withIdentifier: "addresscell", for: indexPath) as! CreateNewAddressTableViewCell
        cell.newAddressButton.addTarget(self, action: #selector(newAddressAction(_:)), for: .touchUpInside)
        return cell
    }
    else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "shippingmethodcell", for: indexPath) as! MethodTableViewCell
        cell.delegate = self
        cell.boolDelegate = self
        cell.shippingTableView.reloadData()
        if shippingRadio == true {
            cell.select = shippingRadio
            cell.boolSelected()
            cell.shippingmethodURL()
            cell.shippingTableView.reloadData()
        }
        else{
            cell.select = methodRadio
            cell.shippingTableView.reloadData()
        }
        return cell
    }
}

在这个单元类中我有api数据并传递到表视图,如代码所示,现在我需要在地址单元格选择期间调用api,任何人都可以帮助我如何实现这个或任何替代方案

 var chekIndex:IndexPath?
    var arrayss =  [String:Any]()
    var keys = [String]()
    let urlString = "http://www.json-generator.com/api/json/get/bVgbyVQGmq?indent=2"
    var delegate: CheckoutDelegate?
    var heightConstant: Int?
    var name = [String]()
    var totalCount = 0
    var radioSelected:Bool?
    var radioSelection: Bool?
    var boolDelegate: BoolValidationDelegate?
    var select:Bool?

    override func awakeFromNib() {
        super.awakeFromNib()
        radioSelection = false
        self.shippingmethodURL()
        shippingTableView.delegate = self
        shippingTableView.dataSource = self
        shippingTableView.rowHeight = UITableViewAutomaticDimension
        shippingTableView.estimatedRowHeight = shippingTableView.rowHeight
        // Initialization code
    }
    func paymentRadioAction(button : KGRadioButton) {
        _ = button.center
        let centralPoint = button.superview?.convert(button.center, to:self.shippingTableView)
        let indexPath =  self.shippingTableView.indexPathForRow(at: centralPoint!)
        if button.isSelected {

        } else{
            chekIndex = indexPath
            radioSelection = true
            self.shippingTableView.reloadData()
            self.boolDelegate?.boolvalidation(bool: radioSelection!)
        }
    }
    func shippingmethodURL() {
        guard let url = URL(string: self.urlString) 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
                self.keys = Array(jsonObj.keys)
                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 {
                    self.shippingTableView.reloadData()
                    let sectionHeight = self.arrayss.count * 31
                    let cellHeight = self.totalCount * 44
                    self.shippingHeightConstraint.constant = CGFloat(sectionHeight + cellHeight)
                    self.heightConstant = Int(self.shippingHeightConstraint.constant)
                    self.delegate?.heightConstant(int: self.heightConstant!)
                }
            }
        }).resume()
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return arrayss.count
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return self.keys[section]
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let key = self.keys[section]
        let a :[Any]  = arrayss[key] as! [Any]
        return a.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        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.methodLabel.text = name  as? String
        cell.priceLabel.text = price as? String
        cell.radioButton.addTarget(self, action: #selector(paymentRadioAction(button:)), for: .touchUpInside)
        if chekIndex == indexPath {
            cell.radioButton.isSelected = true
        } else {
            cell.radioButton.isSelected = false
        }
    return cell
    }

这是三个部分的图像

enter image description here enter image description here

0 个答案:

没有答案