2017-03-03 135 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

提供精确的代码,我已经实现了 –

相关问题