2017-07-04 40 views
1

在這裏,我在表格視圖中有四個部分,但在第一部分中,所有單元格都具有單選按鈕。每當它處於活動狀態時,我需要顯示剩餘的三個部分,如果不是,我需要隱藏剩餘的最後三個部分任何人都可以幫助我如何實現這一點?如何使用單選按鈕隱藏表格視圖中的部分?

圖片如下所示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 
     } 
    } 
+0

可以同時選擇多個單選按鈕嗎? –

+0

不止一個人會在 –

+0

時激活,那麼我需要顯示所有部分,如果不是,我只需要顯示第一部分 –

回答

1

拿一個標誌(布爾我的意思是),它的初始值將是false。現在在單選按鈕的選擇集上它的值爲true

現在,更改numberofSection方法。它將有條件地返回14。我的意思是如果你的國旗是true,那麼返回4否則1!而已!

+0

兄弟我有不同的單元格大小,沒有給行高我可以自動更改單元格大小?@Lion –

+0

我沒有得到你現在問什麼 – Lion

+0

現在在索引路徑的行的代碼高度我給不同的行大小爲不同的部分沒有給這個我可以自動更改它? –

1

這裏您需要根據條件更改numberOfSections方法。您不必將numberOfSections返回爲4,而需要根據單選按鈕選擇進行更改。 你可以保持一個布爾變量來檢查基於該返回按鈕是否被選中與否和基於像

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

這意味着我需要實現單選按鈕動作中的部分數量? –

+0

如何實施,你可以告訴我嗎? –

+0

只保留一個布爾變量來檢查按鈕是否被選中,並根據其值改變numberOfSections方法 –

1

它的價值變化numberOfSections方法保持Bool變量來標識單選按鈕被選中或不countnumber 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 
    } 
    } 
相關問題