在带有自定义单元格的 UITableView 中保存输入?
我很难找到如何为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,这正是这样做的方法。视图永远不应该保存数据。结果是,一旦用户编辑视图,新数据就应该保存在模型中。
不,不在 iPhone 上。 OS X 上的 Cocoa 具有与 NSArrayController 结合的 Cocoa Bindings 形式的类似功能,但此功能在 iOS 上不可用。你必须走手动路线。
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.
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.通常,在可编辑的详细信息视图中,无需重用
UITableViewCell
实例。如果不是在tableView:cellForRowAtIndexPath:
中动态创建单元格,而是在 Interface Builder 或init...
或viewDidLoad
方法中创建单元格,您可以将它们缓存在实例变量中,这样您就不必担心用户数据因滚动而消失。您应该能够使用
NSKeyValueObserving
方法直接观察UITextField
实例的text
属性的更改。您可以利用它来传播更改,尽管您需要在控制器(或其他地方)中实现一些自定义功能以将文本字段绑定到关键路径。Typically, in an editable detail view there's no need to reuse
UITableViewCell
instances. If, instead of creating cells dynamically intableView:cellForRowAtIndexPath:
you create the cells in Interface Builder or in yourinit...
orviewDidLoad
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.You should be able to use
NSKeyValueObserving
methods to directly observe changes to thetext
properties ofUITextField
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.