2017-07-04 3 views
1

ここでは、テーブルビューに4つのセクションがありますが、最初のセクションではすべてのセルにラジオボタンがあります。それがアクティブなときはいつでも、私は残りの3つのセクションを表示する必要があります、私は残りの最後の3つのセクションを非表示にする必要があります誰も私はこれを実装する方法を助けることができますか?ラジオボタンを使用してテーブルビューのセクションを非表示にする方法は?

画像は以下の通りです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

一度にアクティブになるのは1つだけです –

+0

アクティブなときはすべてのセクションを表示する必要があります。最初のセクション –

答えて

1

1つのフラグ(ブールの意味)をとります。初期値はfalseです。今すぐラジオボタンの選択で値をtrueに設定します。

今すぐnumberofSectionメソッドを変更してください。条件付きで1または4を返します。あなたの旗がtrueだったら、4、それ以外の場合は1を返してください。それでおしまい!

+0

私は行の高さを与えずにセルのサイズを変えていますか?セルサイズを自動的に変更できますか?@Lion –

+0

あなたは正確に何を求めているのですか? – Lion

+0

インデックスパスの行のコードの高さに今私はこれを与えずに異なるセクションに対して異なる行サイズを与えました。 –

1

ここでは、条件に応じてnumberOfSectionsメソッドを変更する必要があります。 numberOfSectionsを4に戻す代わりに、ラジオボタンの選択に従ってそれを変更する必要があります。あなたは返すことに基づいて選択するかしないラジオボタンを識別するためにBool変数をキープボタンが選択かと

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

を表示する必要があるということは、ラジオボタンのアクションでセクションの数を実装する必要があることを意味しますか? –

+0

それを実装する方法を教えてくれますか? –

+0

ボタンが選択されているかどうかをチェックし、その値の変更に基づいてブール変数を保持するだけですnumberOfSectionsメソッド –

1

のようにその値変更numberOfSections法に基づいているかどうかをチェックするブール変数を維持することができます 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 
    } 
    } 
関連する問題