在带有自定义单元格的 UITableView 中保存输入?

发布于 2024-10-18 20:04:23 字数 1259 浏览 1 评论 0原文

我很难找到如何为 UITableView 进行真正的数据绑定。我目前有一个表格视图,其中有许多自定义单元格,其中大多数都很简单,其中之一则不然。另外,我会根据需要重用单元格,因此在某些时候,任何给定单元格中的数据都可以滚动到屏幕之外,此时从 UI 角度来看数据会丢失。我需要知道获取此信息并保存它的最佳方法(最佳实践)。

我通常可以在任何给定时间显示大约 4-5 个单元格。

目前,我的所有数据都是通过文本字段输入的(尽管这可能很快就会改变,因为我可能会向其中一个单元格添加切换开关)。每次用户触摸文本字段时,都会调用 textFieldDidEndEditing。然后,我根据文本字段中的一些信息(如标签或占位符文本)保存数据。我这样做是因为我需要在该单元格滚动到视图之外之前获取数据并保存它,此时我无法再通过从 UITextField 获取文本来保存数据。

我忍不住相信有更好的方法来做到这一点。我的第一个想法是找到一种方法来知道给定的单元格即将滚动到视图之外(因此可能会失去它的价值,因为它将被重用),然后保存此时的数据。我在 UITableViewController、UITableView 或 UITableViewCell 类引用中找不到任何暗示此类内容的内容。所以,我猜这不是要走的路。

我的另一个想法是数据绑定,这似乎是最合适的,但我找不到任何好的例子。我需要一种将数据绑定到自定义对象的方法。

例如,我有一个对象,例如“联系人”。该对象有一个用于名称的字符串字段、一个用于注释的字符串字段和一个地址字段(另一个对象),该地址字段具有自己的一组字段(例如 street1、street2、city、state、zip)。我想将这些值绑定到表视图,以便在编辑字段时将其保存到我的内部对象中。

Q1:有办法做到这一点吗?

仅供参考,联系人的字符串字段将共享自定义单元格类型,但地址将获得自己的自定义单元格,其中包含所有文本字段。我不会为地址中的每个字段创建不同的单元格,而是创建一个包含 4 或 5 个文本字段的自定义单元格。

让事情变得更复杂的是,我可能需要更换自定义地址单元格以匹配不同国家/地区的地址单元格。所以现在我需要将地址对象 (ref) 绑定到新的自定义地址单元格。

问题2:这会改变上面问题1的答案吗?

再次,我正在寻找一种更好的方法来保存带有 UITextFields 的自定义 UITableViewCells 中的值,并且我必须考虑数据可以滚动到屏幕外并且单元格可以重用这一事实,因此并非我需要的所有数据都可用当用户退出 UITableView 时对我来说。

非常感谢任何帮助或指导。甚至只是向我指出我尚未找到的好资源。

I'm having a difficult time finding how to do true data binding for UITableViews. I currently have a table view that has a number of custom cells, most of which are simple, and one of which is not. Also, I'm reusing cells as necessary, so at some point, data from any given cell can scroll off the screen, at which point the data is lost from a UI perspective. I need to know the best way (best practice) to get this information and save it.

I can usually show about 4-5 cells at any given time.

Currently all of my data is input via text fields (though this could change soon, as I may add a toggle switch to one of the cells). Each time the user touches out of a text field, textFieldDidEndEditing is called. I then save the data based off of some information from the text field (like tag or placeholder text). I do this because I need to get the data and save it before that cell scrolls out of view, at which point I could no longer save the data by getting the text from the UITextField.

I can't help but believe there is a better way to do this. My first thought was to find a way to know that a given cell is about to scroll out of view (and thus likely lose it's value b/c it will be reused), and just save the data at that point. I can't find anything in the UITableViewController, UITableView, or UITableViewCell class references that hint at anything like this. So, I'm guessing that is not the way to go.

The other thought I had was data binding, which seems the most appropriate, but I can't find any good examples of this. I need to have a way to bind data to custom objects.

For example, I have an object, say, Contact. This object has a string field for name, a string field for notes, and an Address field (another object) that has it's own set of fields (like, street1, street2, city, state, zip). I would like to bind these values to the table view such that when a field has been edited, it is saved to my internal object.

Q1: Is there a way to do this?

FYI, the string fields for Contact will share custom cell types, but the Address will get it's own custom cell with all of the text fields in it. I'm not making different cells for each field in the address, but rather one custom cell with 4 or 5 text fields in it.

To complicate things a little further, I may need to swap out the custom address cell to match that of a different country. So now I need to bind the Address object (ref) to the new custom address cell.

Q2: Does this change the answer to Q1 above?

Again, I'm looking for a better way to save values from custom UITableViewCells with UITextFields, and I have to consider the fact that the data can scroll off the screen and a cell be reused, so not all of the data I need is available to me by the time the user exits the UITableView.

Any help or guidance is greatly appreciated. Even just pointing me to a good resource that I have yet to find.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她比我温柔 2024-10-25 20:04:23

目前,我的所有数据都是通过文本字段输入的(尽管这可能很快就会改变,因为我可能会向其中一个单元格添加切换开关)。每次用户触摸文本字段时,都会调用 textFieldDidEndEditing。然后,我根据文本字段中的一些信息(如标签或占位符文本)保存数据。我这样做是因为我需要在该单元格滚动到视图之外之前获取数据并保存它,此时我无法再通过从 UITextField 获取文本来保存数据。

我不禁相信有更好的方法来做到这一点。

在我看来,这正是这样做的方法。视图永远不应该保存数据。结果是,一旦用户编辑视图,新数据就应该保存在模型中。

我的另一个想法是数据绑定,这似乎是最合适的,但我找不到任何好的例子。我需要一种将数据绑定到自定义对象的方法。

问题1:有办法做到这一点吗?

不,不在 iPhone 上。 OS X 上的 Cocoa 具有与 NSArrayController 结合的 Cocoa Bindings 形式的类似功能,但此功能在 iOS 上不可用。你必须走手动路线。

Currently all of my data is input via text fields (though this could change soon, as I may add a toggle switch to one of the cells). Each time the user touches out of a text field, textFieldDidEndEditing is called. I then save the data based off of some information from the text field (like tag or placeholder text). I do this because I need to get the data and save it before that cell scrolls out of view, at which point I could no longer save the data by getting the text from the UITextField.

I can't help but believe there is a better way to do this.

In my opinion, this is exactly the way to do it. The views should never hold the data. The consequence is that, as soon as the user edits a view, the new data should be saved in the model.

The other thought I had was data binding, which seems the most appropriate, but I can't find any good examples of this. I need to have a way to bind data to custom objects.

Q1: Is there a way to do this?

No, not on the iPhone. Cocoa on OS X has something like this in the form of Cocoa Bindings in combination with NSArrayController, but this feature is not available on iOS. You have to go the manual route.

像你 2024-10-25 20:04:23

此外,我会根据需要重复使用单元格,
所以在某个时刻,来自任何给定的数据
单元格可以滚动离开屏幕,位于
UI 中数据丢失的点
观点。

通常,在可编辑的详细信息视图中,无需重用 UITableViewCell 实例。如果不是在 tableView:cellForRowAtIndexPath: 中动态创建单元格,而是在 Interface Builder 或 init...viewDidLoad 方法中创建单元格,您可以将它们缓存在实例变量中,这样您就不必担心用户数据因滚动而消失。

我想将这些值绑定到
表视图这样当一个字段
已编辑,已保存至我的
内部对象

您应该能够使用 NSKeyValueObserving 方法直接观察 UITextField 实例的 text 属性的更改。您可以利用它来传播更改,尽管您需要在控制器(或其他地方)中实现一些自定义功能以将文本字段绑定到关键路径。

Also, I'm reusing cells as necessary,
so at some point, data from any given
cell can scroll off the screen, at
which point the data is lost from a UI
perspective.

Typically, in an editable detail view there's no need to reuse UITableViewCell instances. If, instead of creating cells dynamically in tableView:cellForRowAtIndexPath: you create the cells in Interface Builder or in your init... or viewDidLoad methods, you can cache them in instance variables so you won't have to worry about user data disappearing as a side-effect of scrolling.

I would like to bind these values to
the table view such that when a field
has been edited, it is saved to my
internal object

You should be able to use NSKeyValueObserving methods to directly observe changes to the text properties of UITextField instances. You could take advantage of that to propagate changes, though you'd need to implement some custom functionality in your controller (or elsewhere) to bind the text fields to key paths.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文