[英]How to maintain tableview scroll to stop reusing cell
I have multiple sections in tableview.
我在表视图中有多个部分。 I have multiple questions and multiple answers of each question.
我有多个问题,每个问题有多个答案。 In multiple answers, I have one option and that is other (option).
在多个答案中,我有一个选择,而另一个是(选择)。 when I select the button of other, then it shows the text field for advice.
当我选择其他按钮时,它会显示文本字段以供参考。 Now i need to maintain the data of text field and that selected option's (Other) text when scrolling in tableview.I am using below code for all answer.
现在我需要在表视图中滚动时维护文本字段和所选选项的(Other)文本的数据。我在下面的代码中使用所有答案。
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
}
You will have to retain (store in some dictionary) the data entered in textfield, otherwise it will be lost when you scroll table and cell is reloaded. 您将必须保留(存储在某些词典中)在文本字段中输入的数据,否则在滚动表和重新加载单元格时这些数据将丢失。 If you don't want to retain then you should use scroll view instead of table view.
如果您不想保留,则应使用滚动视图而不是表格视图。 In scrollview it will not redraw UI even if you scroll up and down.
在scrollview中,即使您上下滚动它也不会重绘UI。
You need to separate UI and data. 您需要将UI和数据分开。 You embed data into your cell and when cells are reused, you lost data.
您将数据嵌入到单元格中,当单元被重用时,您将丢失数据。
You can do 2 things: 您可以做两件事:
ViewModel
class which contains data of cell: text, color, etc. Of course you need to update your ViewModel
as you receive input You can google "MVVM pattern" for more information. ViewModel
类,其中包含单元格数据:文本,颜色等。当然,您需要在接收输入时更新ViewModel
。 Even if your cell is reused, your data is safe in ViewModel
object. ViewModel
对象中也是安全的。 Array
so that they won't be reused. Array
以免重复使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.