UITableView状态持久性中的复选框 [英] Checkboxes in UITableView State Persistence

查看:65
本文介绍了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状态持久性中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆