2017-06-21 22 views
0

這裏我實現了一個自定義設計,如圖像shown here我需要激活單選按鈕取決於我的Web服務數據。在Web服務中,一個關鍵值對我只能接收0 & 1,如果它是1,那麼我需要激活單選按鈕,如何實現該幫助?如何啓用單選按鈕取決於來自Web服務的數據?

這裏是我的單選按鈕

 @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 
+0

我沒有從您那裏得知您是否需要使用API​​數據顯示單選按鈕的狀態。或者你想單擊它時更新單選按鈕狀態? –

+0

我需要兩個,但第一次的應用程序加載,然後它應該得到API,並根據我的數據使活動單選按鈕,稍後我需要手動更改它 –

+0

手動我已經設置,我需要設置api數據也@DSDharma –

回答

0

更改下面的代碼行的代碼。

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() 

    } 
+0

我沒有得到radiobuttonkey作爲keyvalue對,然後它是如何工作 –

+0

我得到默認爲keyvalue對,因爲我需要設置爲radiobutton –

+0

讓checkIndex = self.checkIsRadioSelect.index(self.checkIsRadioSelect.index of:indexPath.row)我正在使用此手動更改單選按鈕鍵以激活 –

0

在你cellForRowAtIndexPath方法的更新代碼像下面

我的建議是不要設置爲indexPath.row標籤同時重用的tableView細胞它不會給你更好的result.Make一些獨特的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 
    } 
+0

我需要替換或添加它那裏的方法 –

+0

替換您的舊單選按鈕與它的代碼。請務必檢查默認值是Bool或Int –

+0

這意味着我需要在單選按鈕操作方法或單元格中替換它對於在indexpth的行 –

相關問題