如何存储发生在 UITableView 单元格中的用户交互 UI 元素的更改 [英] How to store changes to user-interaction UIelements occurring within UITableView cells

查看:17
本文介绍了如何存储发生在 UITableView 单元格中的用户交互 UI 元素的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近意识到在使用 UITableView 时,我应该管理实际单元格对象之外的所有单元格数据(为了保留"我的数据,因为单元格被重用).然后,当我需要在我的 cellForRowAt 方法中为我的 tableview 提供一个单元格时,我只需将持久化"设置为数据到我重复使用的单元格.

I have recently become aware that when using a UITableView, I should be managing all my cells' data outside of the actual cell objects (to "persist" my data since cells are reused). Then, when I need to provide a cell to my tableview in my cellForRowAt method, I simply set that "persisted" data to my reused cell.

但是,我对如何在我的单元格中存储用户交互 UI 元素的更改感到特别困惑?(例如,如果 UILabel 的 alpha 值从默认值 1.0 更改为新值 0.5)

However, I am confused specifically on how to store changes in user-interaction UIelements in my cells? (For example, if a UILabel's alpha value changes from a default 1.0 to a new value of 0.5)

以下是我的一些可能的想法(我不相信这些想法是正确的,因此欢迎提出任何批评或建议):

Here are a couple possible ideas I've had (which I'm not convinced are correct, so any critiques or suggestions are welcome):

  1. 在单元格的数据模型中完全复制单元格的每个 IBOutlet 变量(这意味着我将在 UITableViewCell 中有一个 UILabel,在我的 CellModel 中有一个持久副本).然后我可以将这些整个变量复制回 cellForRowAt 方法中的单元格.然而,这似乎有点多余,而且浪费内存.

  1. Replicate each IBOutlet variable of the cell entirely in the cell's data model (meaning I would have a UILabel in the UITableViewCell, and a persisted copy in my CellModel). Then I can just copy these entire variables back to the cell in the cellForRowAt method. However, this seems kind of redundant and a waste of memory.

只需记录我的单元格模型中的具体变化.例如,如果 UILabel 的 alpha 值发生变化,只需记录该特定的新 alpha 值,而不是整个 UILabel.然而,我看到的问题/困惑是,我似乎可能只为一个元素记录了很多东西(如果不仅仅是一个特定元素的 alpha 变化).我也对如何实际实现这一点感到困惑(所以这可能比我想象的要容易得多?).

Just record the specific change in my cell' model. For example, if an alpha value of a UILabel changes, just record that specific new alpha value, not the whole UILabel. However, the problem/confusion I'm seeing with this is that it seems like I might be recording a lot of things for just one element (if more than just an alpha changes for a particular element). I am also confused on how I should actually implement this (so maybe this could be a lot easier than I'm visualizing?).

感谢指导

ps.我使用故事板

推荐答案

永远不会有需要在单元数据模型中存储 UITableViewCell UI 变量状态的情况,你可以这样做,但非常不鼓励这样做.

There should never be a case where you would need to store UITableViewCell UI variable state inside cell data model, you can do it tho, but it is highly discouraged.

通常您的 UI 状态(不仅是 UITableViewCells,任何 UI 元素)取决于它代表的某些数据模型.因此,在您的情况下,单元格的数据模型可以有一个布尔值/枚举或其他任何东西来表示单元格的状态,您可以根据它们的数据模型状态组合您的 UITableViewCell.

Usually your UI state (not only UITableViewCells, any UI element) depends on some data model it represents. So, in your case, data model of a cell could have a boolean/enum or whatever else to represent a state of a cell and you could compose your UITableViewCell's depending on their data model states.

这篇关于如何存储发生在 UITableView 单元格中的用户交互 UI 元素的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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