如何使用单选按钮隐藏表格视图中的部分?

时间:2017-07-04 09:06:30

标签: ios uitableview swift3

这里我在表格视图中有四个部分,但在第一部分中,所有单元格都有单选按钮。每当它处于活动状态时,我需要显示剩余的三个部分,如果不是我需要隐藏剩余的最后三个部分,任何人都可以帮助我如何实现这个部分吗?

图片如下所示enter image description here

 @IBAction func selectRadioButton(_ sender: KGRadioButton) {
        let chekIndex = self.checkIsRadioSelect.index(of: sender.tag)
        if sender.isSelected {

        } else{
            if(chekIndex == nil){
                self.checkIsRadioSelect.removeAll(keepingCapacity: false)
                self.checkIsRadioSelect.append(sender.tag)
                self.ProductTableView.reloadData()
            }
        } 
    }

    func numberOfSections(in tableView: UITableView) -> Int{
     return 4
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if (section == 0){
            return "PAYMENT INFORMATION"
        }
        else if (section == 1){
            return "ORDER REVIEW"
        }
        else if (section == 2){
            return "PRODUCT"
        }
        else{
            return ""
        }
    }
    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
        let header = view as! UITableViewHeaderFooterView
        header.textLabel?.textColor = UIColor.gray
        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 paymentmethodsArray.count
        }
        else if (section == 2) {
            return productsDetails.count
        }
        else{
            return 1
        }
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
        if (indexPath.section == 0){
            return 44
        }
        else if (indexPath.section == 1){
            return 410
        }
        else if (indexPath.section == 2){
            return 120
        }
        else{
            return 230
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if (indexPath.section == 0){
            let paymentcell = tableView.dequeueReusableCell(withIdentifier: "paymentcell",for:indexPath) as! paymentTableViewCell
            myActivityIndicator.stopAnimating()
            let arr = self.paymentmethodsArray[indexPath.row]
            paymentcell.paymentNameLabel.text = arr["name"]as? String
            paymentcell.radioButton.tag = indexPath.row
            let checkIndex = self.checkIsRadioSelect.index(of: indexPath.row)
            if(checkIndex != nil){
                paymentcell.radioButton.isSelected = true
            }else{
                paymentcell.radioButton.isSelected = false
            }
            return paymentcell
        }
        else if (indexPath.section == 1){
            let shippingAddresscell = tableView.dequeueReusableCell(withIdentifier: "shippingaddresscell",for:indexPath) as! ShippingAddressTableViewCell
            shippingAddresscell.nameLabel.text = name
            shippingAddresscell.addressLabel.text = billingaddress
            shippingAddresscell.mobileNumberLabel.text = String(describing: phoneNumber)
            shippingAddresscell.shippingNameLabel.text = name
            shippingAddresscell.shippingAddressLabel.text = billingaddress
            shippingAddresscell.shippingMobileNumberLabel.text = String(describing: phoneNumber)
            shippingAddresscell.shippingMethodNameLabel.text = shippingMethod
            shippingAddresscell.paymentMethodNameLabel.text = paymentMethod
            return shippingAddresscell
        }
        else if (indexPath.section == 2){
            let productNamecell = tableView.dequeueReusableCell(withIdentifier: "productNamecell",for:indexPath) as! ProductNameTableViewCell
            let arr = productsDetails[indexPath.row]
            let array = arr["product name"] as! String
            productNamecell.productNameLabel.text = array
            productNamecell.priceLabel.text = arr["Price"] as! String
            productNamecell.QuantityLabel.text = String(describing: arr["Quantity"] as! Int)
            productNamecell.subTotalLabel.text = arr["SubTotal"] as! String
            return productNamecell
        }
        else{
            let ordercell = tableView.dequeueReusableCell(withIdentifier: "ordercell",for:indexPath) as! orderTableViewCell
            ordercell.subTotalLabel.text = subTotal
            ordercell.shippingLabel.text = shippingHandling
            ordercell.grandTotalLabel.text = total
            return ordercell
        }
    }

3 个答案:

答案 0 :(得分:1)

您需要根据条件更改numberOfSections方法。而不是将numberOfSections返回为4,您需要根据单选按钮选择更改它。 你可以保留一个布尔变量来检查按钮是否被选中,并根据它的值变化numberOfSections方法,如

func numberOfSections(in tableView: UITableView) -> Int{
let numberOfRows = ifRadioButtonSelected ? 4 : 1
     return numberOfRows
    }

答案 1 :(得分:1)

保持Bool变量以确定是否选择了单选按钮,具体取决于count number of section的返回。

 var isRadioButtonSelected = false

 @IBAction func selectRadioButton(_ sender: KGRadioButton) {

  //Your remaining logic here.

  //Based on the selection action you have to update the flag value.
  if sender.isSelected {
     isRadioButtonSelected =  true
  } else {
     isRadioButtonSelected =  false
  }

    // Reload table
    self.ProductTableView.reloadData()
 }

 func numberOfSections(in tableView: UITableView) -> Int{
    if  isRadioButtonSelected ==  true {
      return 4
    } else {
      return 1
    } 
  }

答案 2 :(得分:1)

拿一面旗帜(我的意思是布尔),它的初始值为false。现在,在单选按钮的选择中将其值设置为true

现在,更改numberofSection方法。它会有条件地返回14。我的意思是,如果您的旗帜为true,则返回4其他1!就是这样!