2017-03-03 140 views
0

enter image description here我在tableview中有多個部分。我有多個問題和每個問題的多個答案。在多個答案中,我有一個選項,這是其他(選項)。當我選擇其他的按鈕時,它會顯示建議的文本字段。現在,我需要在tableview中滾動時維護文本字段的數據和選定選項的(其他)文本。我使用下面的代碼來解答所有答案。如何保持tableview滾動以停止重用單元格

if (indexPath.section == 2) 
     { 
      let cellidentifier="cell3" 
      let cell=tableView.dequeueReusableCell(withIdentifier: cellidentifier,for:indexPath as IndexPath) as! TextfieldTableViewCell 

      let object_3:AnswerBaseClass = arrobject_answer[0][indexPath.row] 
      //print("arrobject is\(arrobject_answer[0][indexPath.row])") 

      if object_3.answer == "O" 
      { 
        // cell.lbl_answer.isHidden = true 
        cell.btn_selected.isHidden=true 
        //cell.lbl_answer_height.constant = 0 
        cell.Other_textfield.tag = 101 
        cell.Other_textfield.borderStyle = .line 
        cell.Other_textfield_top.constant = -30 
        cell.Height_2.constant = 30 
      } 
      else 
      { 
       cell.lbl_answer?.text = object_3.answer! 
       cell.Other_textfield_top.constant = 12 
       cell.Height_2.constant = 0 
       cell.lbl_answer.isHidden = false 
       cell.btn_selected.isHidden=false 
       if answer_main_data[0][indexPath.row] == true 
       { 
        cell.lbl_answer.tag = indexPath.row 
        cell.btn_selected.isSelected=true 
        if cell.lbl_answer.text == "Other" 
        { 
         for subview in cell.contentView.subviews 
         { 
          subview.removeFromSuperview() 
         } 
         if arrOtherTextfield_2.indices.contains(indexPath.row) 
         { 
          cell.addSubview(arrOtherTextfield_2[indexPath.row]) 
         } 
         else 
         { 
          cell.Other_textfield.tag = 1100 
          cell.Other_textfield.borderStyle = .line 
          cell.Height_2.constant = 30 
          arrOtherTextfield_2.append(cell.Other_textfield) 
         } 
        } 
        else 
        { 
         cell.Height_2.constant = 0 
        } 
       } 
       else 
       { 
        cell.Other_textfield_top.constant = 12 
        cell.btn_selected.isSelected=false 
        cell.Height_2.constant = 0 
       } 
      } 
      cell.Other_textfield.borderStyle = .line 
      return cell 
     } 
+0

你可以添加你想要的截圖嗎? – Ocunidee

+0

我已添加屏幕截圖 –

+0

它仍然不清楚你的意思是維護數據 – Ocunidee

回答

0

您必須保留(在一些字典店)在文本框輸入的數據,否則當您滾動表將丟失,細胞被重新加載。如果你不想保留,那麼你應該使用滾動視圖而不是表格視圖。在滾動視圖中,即使您上下滾動,也不會重新繪製UI。

+0

PLZ提供確切的代碼,我實現了什麼 –

0

您需要分離UI和數據。您將數據嵌入到單元格中,並且當單元格被重用時,您會丟失數據。

你可以做兩件事情:

  1. 創建ViewModel類,其中包含細胞的數據:文本,顏色等,當然你需要爲你接收輸入可以在Google更新您的ViewModel中「MVVM模式「 瞭解更多信息。即使您的單元格被重用,您的數據在ViewModel對象中也是安全的。
  2. 您可以將您的單元格保存在Array中,以便它們不會被重複使用。
+0

提供精確的代碼,我已經實現了 –

相關問題