【问题标题】:how to enable the radio button depending on the data coming from web services?如何根据来自 Web 服务的数据启用单选按钮?
【发布时间】:2017-06-21 14:00:24
【问题描述】:

在这里我实现了一个自定义设计,如图像shown here 我需要根据我的 Web 服务数据激活单选按钮。在 Web 服务中,我只收到 0 和 1 的一个键值对,如果它是 1,那么我需要激活单选按钮来帮助如何实现?

这是我的单选按钮的代码

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

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

这是表格视图的代码

       let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! addressTableViewCell
                tableDetails.isHidden = false
                myActivityIndicator.stopAnimating()
                let arr = detailsArray[indexPath.row]
                cell.nameLabel.text = arr["name"]as? String
                cell.addressLabel.text = arr["address"]as? String
                let mobilenumber : Int =  arr["number"] as! Int
                cell.mobileNumberLabel.text = String(describing: mobilenumber)
                let defaultaddress : Int = arr["default"] as! Int
                cell.radioButton.tag = indexPath.row
                cell.editButton.tag = indexPath.row
                cell.deleteButton.tag = indexPath.row
                cell.editButton.isHidden = true
                cell.deleteButton.isHidden = true
                let checkIndex = self.checkIsRadioSelect.index(of: indexPath.row)
                if(checkIndex != nil){
                    cell.radioButton.isSelected = true
                    cell.editButton.isHidden = false
                    cell.deleteButton.isHidden = false
                }else{
                    cell.radioButton.isSelected = false
                    cell.editButton.isHidden = true
                    cell.deleteButton.isHidden = true
                }
                return cell

【问题讨论】:

  • 我没有告诉你是否需要使用 API 数据显示单选按钮的状态。或者您想在单击时更新单选按钮状态?
  • 我需要两者都有,但对于第一次加载应用程序,它应该从 api 获取并根据我的数据激活单选按钮,稍后我需要手动更改它
  • 手动我已经设置,我还需要设置 api 数据@DSDharma
  • 检查我的答案..

标签: ios uitableview swift3


【解决方案1】:

在您的cellForRowAtIndexPath 方法更新代码中,如下所示

我的建议是不要在重用 tableView 单元格时将 indexPath.row 设置为标签,它不会给你更好的结果。将一些唯一的 id 作为标签。或者做一些数学计算,加/乘数字。

 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! addressTableViewCell
 tableDetails.isHidden = false
 myActivityIndicator.stopAnimating()
 let arr = detailsArray[indexPath.row]
 cell.nameLabel.text = arr["name"]as? String
 cell.addressLabel.text = arr["address"]as? String
 let mobilenumber : Int =  arr["number"] as! Int
 cell.mobileNumberLabel.text = String(describing: mobilenumber)
 cell.radioButton.tag = indexPath.row
 cell.editButton.tag = indexPath.row
 cell.deleteButton.tag = indexPath.row
 cell.editButton.isHidden = true
 cell.deleteButton.isHidden = true
 cell.radioButton.isSelected = false

 if let isDefault = arr["default"] as? Bool { // i think it won't be Integer it will be Bool,please debug & check it
          cell.radioButton.isSelected = isDefault 
          cell.editButton.isHidden = false
          cell.deleteButton.isHidden = false
 }
 let checkIndex = self.checkIsRadioSelect.index(of: indexPath.row)
     if(checkIndex != nil){
     cell.radioButton.isSelected = true
     cell.editButton.isHidden = false
     cell.deleteButton.isHidden = false
  }

【讨论】:

  • 我需要在那个方法中替换或添加它
  • 用它替换单选按钮的旧代码。确保检查默认值为 Bool 或 Int
  • 这意味着我需要在单选按钮操作方法或单元格中将其替换为 indexpth 处的行
  • 是的,我不知道 self.checkIsRadioSelect 的用途请更新它
  • 如果我更改了,我将无法手动更新激活的单选按钮
【解决方案2】:

更改下面的代码行。

let checkIndex = self.checkIsRadioSelect.index(of: indexPath.row) //delete this line

let checkIndex = arr["default"] as! Int //use radio button key.

@IBAction func selectRadioButton(_ sender: KGRadioButton) {

        let arr = detailsArray[sender.tag]
        if sender.isSelected {
            arr["default"] = @"0"

        } else{
            arr["default"] = @"1"
        }
        self.tableDetails.reloadData()

    }

【讨论】:

  • 我没有将单选按钮键作为键值对,那么它是如何工作的
  • 我将默认设置为键值对,因为我需要设置为单选按钮
  • let checkIndex = self.checkIsRadioSelect.index(of: indexPath.row) 我正在使用它手动将单选按钮键更改为活动
  • 关键是什么?你能分享网络服务响应吗?
  • 默认是我的键值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-02
  • 1970-01-01
  • 2017-06-30
  • 2017-02-05
  • 1970-01-01
  • 2020-02-21
  • 1970-01-01
相关资源
最近更新 更多