UITableView 状态持久化中的复选框

Checkboxes in UITableView State Persistence(UITableView 状态持久化中的复选框)

本文介绍了UITableView 状态持久化中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个包含选项单元格的表格,每个单元格都有一个绑定标签和 BEMCheckbox 库提供的复选框.一切正常,但是,当我选择前三个选项并向下滚动时,我发现选择了其他三个相邻的单元格,即使我还没有遇到它们.

So I have a table which contains option cells, each of which has a bound label and check box provided by the BEMCheckbox library. Everything works fine, however, when I select let's say the first three options and scroll down, I find three other adjacent cells selected even though I have yet to encounter them.

我知道这与 UITableView 的内部工作原理以及单元格在滚动时基本上如何重用和重新渲染有关,但我没有尝试保持状态似乎有效.目前我正在尝试将选定的 IndexPath 对象存储在一个数组中,并且在 cellForRowAt 中,我将绘制复选框,就像路径出现在数组中一样.但是,即使这样似乎也不起作用.任何想法如何解决它?

I understand this has something to do with the inner workings of UITableView and how cells are basically reused and re-rendered as they are scrolled, but no attempt I make to persist state seems to be working. At the moment I'm trying to store selected IndexPath objects in an array, and in the cellForRowAt I will draw the checkbox as on if the path appears in the array. However, even this doesn't seem to work. Any ideas how to fix it?

推荐答案

将表格视图单元视为您在医生办公室候诊室获得的患者信息表格.问题是医生办公室会重复使用这些表格,并且不会删除其中的旧信息.

Think of a table view cell as the patient info form you get in the waiting room at a doctor's office. The catch is that the doctor's office reuses the forms, and doesn't erase the old info from them.

在您的 cellForRowAt 方法中,您可以在再次使用之前完全将单元格恢复到其默认状态.您必须将所有内容重置为默认状态.

In your cellForRowAt method, it's up to you to completely restore the cell to its default state before using it again. You have to reset everything to it's default state.

作为在 cellForRowAt 中进行重置的替代方法,您可以使用自定义单元格并实现 prepareForReuse() 方法.

As an alternative to doing that resetting in cellForRowAt, you can use a custom cell and implement the prepareForReuse() method.

关键是在开始配置之前,您需要将单元格中的每个视图强制设为默认状态.

The key thing is that you need to force every view in the cell to it's default state before you begin configuring it.

这篇关于UITableView 状态持久化中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:UITableView 状态持久化中的复选框

基础教程推荐