简体   繁体   English

滚动uitableview时图像恢复为原始图像

[英]image is reverting to original when scrolling uitableview

I have a button inside each cell. 我在每个单元格内都有一个按钮。 When it's pressed, the image is changed (basically a checkbox) to denote a selection. 按下时,图像会更改(基本上是一个复选框)以表示选择。 When you scroll to the bottom ... then scroll back up to the top. 当您滚动到底部时...然后向上滚动到顶部。 The image is reverted to the original image. 图像将还原为原始图像。

This question is pretty similar to this: 这个问题与此非常相似:

Preserve Cell Image After Scrolling UITableView 滚动UITableView后保留单元格图像

And others. 和别的。 But, I can't seem to find a good answer. 但是,我似乎找不到一个好的答案。 I understand that's it's reverting back to how the uitableview is setup when the cell goes off the screen. 我知道那是当单元格离开屏幕时,它又恢复为uitableview的设置方式。 But, how do I save the changed image to the uitableview so when it scrolls it doesn't revert to the original? 但是,如何将更改的图像保存到uitableview,以便在滚动时不会恢复为原始图像?

Thanks in advance! 提前致谢! =) =)

It's changing back because cells are reused. 由于单元已被重用,它正在变回原样。 When your cell is going off the screen it is taken out of the view and put back into the reuse pool. 当您的单元格离开屏幕时,它将从视图中移出并放回到重用池中。 Then you're getting it out of the queue again in cellForRowAtIndexPath and setting it back up as the default. 然后,您将其再次从cellForRowAtIndexPath移出队列,并将其设置为默认值。

The question you linked to is exactly what you should follow. 您链接到的问题正是您应该遵循的。 You should store the state of your cell in your view controller and then when you set it up again in cellForRowAtIndexPath you should load that state and set up the cell appropriately. 您应该将单元格的状态存储在视图控制器中,然后在cellForRowAtIndexPath再次对其进行设置时,应该加载该状态并适当地设置单元格。

One simple way for your method would be to have an NSArray which you set up to be the same size as the number of rows in your table and then in that just store an NSNumber for each row which contains a boolean value on or off for your selection state. 一种简单的方法是将NSArray设置为与表中的行数相同的大小,然后只为每行存储一个NSNumber ,其中包含用于您的布尔值的开或关选择状态。 When the user toggles, toggle the value in the array and then in cellForRowAtIndexPath read that value and set it up appropriately. 当用户切换时,切换数组中的值,然后在cellForRowAtIndexPath读取该值并进行适当设置。

I'm assuming the checkbox in your table view cell is changing state to a selected state because a user selected it. 我假设您的表格视图单元格中的复选框正在将状态更改为选定状态,因为用户已将其选中。 You shouldn't use UI elements to maintain the state of your app. 您不应该使用UI元素来维护应用程序的状态。 That is, when the user taps the checkbox, you should use that event to somehow reflect that state change in a data object in your app. 也就是说,当用户点击复选框时,您应该使用该事件以某种方式在应用程序的数据对象中反映该状态更改。 Then, when that cell needs to be displayed again, you configure it with the state you previously saved. 然后,当该单元格需要再次显示时,可以使用先前保存的状态对其进行配置。 This allows for things like cell reuse, and view unloading and is all-around a good habit. 这允许像单元重用,视图卸载之类的事情,并且是一个全面的好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM