【问题标题】:How to hide sections in a table view using a radio button?如何使用单选按钮隐藏表格视图中的部分?
【发布时间】:2017-07-04 09:06:29
【问题描述】:

在这里,我在表格视图中有四个部分,但在第一部分中,所有单元格都有单选按钮。每当它处于活动状态时,我需要显示剩余的三个部分,否则我需要隐藏剩余的最后三个部分,谁能帮我实现这个?

图片如下图 ?

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

【问题讨论】:

  • 可以同时选择多个单选按钮吗?
  • 一次不会只有一个处于活动状态
  • 当它处于活动状态时,我需要显示所有部分,如果不是,我只需要显示第一部分

标签: ios uitableview swift3


【解决方案1】:

取一个标志(我的意思是布尔值),它的初始值为false。现在在单选按钮的选择中将其值设置为true

现在,更改numberofSection 方法。它将有条件地返回14。我的意思是如果你的标志是true 然后返回4 否则1!就是这样!

【讨论】:

  • 兄弟我有不同的单元格大小而没有给出行高我可以自动更改单元格大小吗?@Lion
  • 我没有得到你的确切要求
  • 现在在索引路径行的代码高度中,我为不同部分给出了不同的行大小,但没有给出这个我可以自动更改吗?
  • 这意味着如果第二部分增加了,那么如何自动更改行高
  • 你应该看看UITableViewAutomaticDimension
【解决方案2】:

保留Bool 变量以根据返回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
    } 
  }

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 这意味着我需要在单选按钮操作中实现多个部分?
    • 你能告诉我如何实现吗?
    • 只保留一个布尔变量来检查按钮是否被选中,并根据其值更改 numberOfSections 方法
    猜你喜欢
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多