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

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

图像如下所示 在这里输入图像说明

@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 } } 

拿一个标志(布尔我的意思),它的初始值将是false 。 现在在单选button的select设置它的值为true

现在,更改段的方法。 它将有条件地返回14 。 我的意思是如果你的国旗是true那么返回4其他1 ! 而已!

保持Boolvariables来标识单选button是否被选中或不是基于返回的number of sectioncount

  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 } } 

这里你需要根据条件改变numberOfSections方法。 而不是返回numberOfSections为4,你需要根据单选button来改变它。 您可以保留一个布尔variables来检查button是否被选中,并根据其值改变numberOfSections方法

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