問題描述
所以我有一個包含選項單元格的表格,每個單元格都有一個綁定標簽和 BEMCheckbox 庫提供的復選框.一切正常,但是,當我選擇前三個選項并向下滾動時,我發(fā)現(xiàn)選擇了其他三個相鄰的單元格,即使我還沒有遇到它們.
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 的內(nèi)部工作原理以及單元格在滾動時基本上如何重用和重新渲染有關,但我沒有嘗試保持狀態(tài)似乎有效.目前我正在嘗試將選定的 IndexPath 對象存儲在一個數(shù)組中,并且在 cellForRowAt 中,我將繪制復選框,就像路徑出現(xiàn)在數(shù)組中一樣.但是,即使這樣似乎也不起作用.任何想法如何解決它?
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?
推薦答案
將表格視圖單元視為您在醫(yī)生辦公室候診室獲得的患者信息表格.問題是醫(yī)生辦公室會重復使用這些表格,并且不會刪除其中的舊信息.
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 方法中,您可以在再次使用之前完全將單元格恢復到其默認狀態(tài).您必須將所有內(nèi)容重置為默認狀態(tài).
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 中進行重置的替代方法,您可以使用自定義單元格并實現(xiàn) prepareForReuse()
方法.
As an alternative to doing that resetting in cellForRowAt, you can use a custom cell and implement the prepareForReuse()
method.
關鍵是在開始配置之前,您需要將單元格中的每個視圖強制設為默認狀態(tài).
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 狀態(tài)持久化中的復選框的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!