2017-10-09 58 views
0

我有一個表視圖,其中有複選框的項目。用戶可以選擇任何項目(多選也),他們可以按繼續按鈕付費。表視圖複選框不接受對索引0的操作

我正在使用一個按鈕作爲複選框。所以如果用戶按下按鈕,或者他們選擇了一個表格行,那麼這兩種情況都會被處理。

所以現在,我的繼續buton開始禁用。每當用戶使用didSelectRowAt方法或複選框按鈕操作按下任何項目時,只有繼續按鈕將被啓用。

但現在,無論何時按下第一個項目或第一個複選框按鈕,我的繼續按鈕都不會啓用。如果我按第二個項目或第二個複選框,它將起作用。我不知道爲什麼。

這裏是我的didSelectRowAt代碼和我的複選框按鈕動作代碼:

#Updated:

var selectedIndexPathArray = Array<NSIndexPath>() 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "itemcell", for: indexPath) as! itemTableViewCell 


     cell.checkboxBtn.isUserInteractionEnabled = false 

     if selectedIndexPathArray.contains(indexPath as NSIndexPath) { 
      cell.checkboxBtn.setImage(UIImage(named: "enabled"), for: .normal) 
      proceedbutton.isUserInteractionEnabled = true 


     } else { 
      cell.checkboxBtn.setImage(UIImage(named: "disabled"), for: .normal) 
      proceedbutton.isUserInteractionEnabled = false 

     } 
     return cell 
    } 



    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

     if selectedIndexPathArray.contains(indexPath as NSIndexPath) { 

      let index = selectedIndexPathArray.index(of: indexPath as NSIndexPath) 
      selectedIndexPathArray.remove(at: index!) 
     } else { 
      selectedIndexPathArray.append(indexPath as NSIndexPath) 
     } 
     tableView.reloadData() 
    } 

提前感謝!

+0

你應該在模型中包含了'selected'狀態,而不是使用一個額外的數組和斯威夫特不使用'NSIndexPath' 3+ – vadian

+0

@vadian我不知道,我怎麼可以更新我的解決方案可以ü請更新 – david

+0

請參閱https://stackoverflow.com/questions/46397812/how-to-make-checkmark-to-be-selected-depending-on-the-array-in-swift-3/46398755#46398755和https://stackoverflow.com/questions/44699371/how-to-save-the-state-of-the-checkbox-to-core-data-in-swift/44704524#44704524有關示例如何包含選擇信息在模型 – vadian

回答

0

是的,因爲第一次如果你點擊按鈕然後proceedbutton將被調用,並在那個時候selectedIndexPathArray將沒有任何值意味着無。

所以第一次你的其他部分將被執行。

. 
. 
. 
else {  
      selectedIndexPathArray.append(indexPath! as NSIndexPath) 
      cell?.checkboxBtn.setImage(UIImage(named: "disabled"), for: .normal) 
      proceedbutton.isUserInteractionEnabled = false 

     } 

而當你按下阿恩那麼你如果條件將是true,因此這將是使

+0

ia m不是得到你的解決方案..你可以請更新完整的代碼.. wr我需要把這個其他部分 – david

+0

現在我應該需要做的,做我期望? – david

+0

你可以發佈完整的解決方案,我replease的變化。然後我也沒有得到 – david

0

試試這個對你進行()函數,你的if-else條件。

if selectedIndexPathArray.contains(indexPath! as NSIndexPath) { 
     electedIndexPathArray.remove(at: index!) 
     cell?.checkboxBtn.setImage(UIImage(named: "disabled"), for: .normal) 
     proceedbutton.isUserInteractionEnabled = false 
    } 
    else { 

     selectedIndexPathArray.append(at: index!) 
     cell?.checkboxBtn.setImage(UIImage(named: "enabled"), for: .normal) 
     proceedbutton.isUserInteractionEnabled = true 
    } 
+0

我,得到相同的錯誤 – david

+0

仍然是我的問題在那裏,無法解決 – david

0

刪除單元格中按鈕的選擇器和默認設置按鈕的userInteraction爲false。無需每次都設置。執行didSelectRow上的所有內容。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCell(withIdentifier: itemscell, for: indexPath) as! itemsTableViewCell 

    cell?.checkboxBtn.isUserInteractionEnabled = false 

     if selectedIndexArray.contains(indexPath.row) { 
      cell.checkboxBtn.setImage(UIImage(named: "enabled"), for: .normal) 
      proceedbutton.isUserInteractionEnabled = true 

     } else { 
      cell.checkboxBtn.setImage(UIImage(named: "disabled"), for: .normal) 
      proceedbutton.isUserInteractionEnabled = false 

     } 
     return cell 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     if selectedIndexArray.contains(indexPath.row) { 
      let index = selectedIndexPathArray.index(of: indexPath.row) 
      selectedIndexArray.remove(at: index!) 
     } else { 
      selectedIndexArray.append(indexPath.row) 
     } 
     tableView.reloadData() 
    } 
+0

那麼我怎麼能禁用並啓用proceedbutton。這個繼續按鈕就像單元格複選框按鈕動作只有 – david

+0

我是,得到同樣的錯誤 – david

+0

仍然是那裏的問題。任何其他解決方案,請 – david