从 TableViewCell 上的 UITextField 获取文本 [英] Get text from UITextField on a TableViewCell

查看:22
本文介绍了从 TableViewCell 上的 UITextField 获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有包含 UITextFields 的原型单元格的 UITableViewController.为了配置这些客户单元,我创建了一个 UITableViewCell 子类.我已经通过出口 (nonatomic, weak) 将 textField 连接到单元格子类.

I have a UITableViewController with prototype cells containing UITextFields. To configure these custome cells, I've created a UITableViewCell subclass. I've conected the textField to the cell subclass via an outlet (nonatomic, weak).

在这个子类上,我创建了一个协议,UITableViewController 是它的代理,这样每次在这些 textFields 中发生变化时,TableViewController 都会知道它.基本上我希望这可以保存 NSUserDefaults 上的值

On this subclass I've created a protocol for which the UITableViewController is its delegate so that everytime something changes in these textFields, the TableViewController knows about it. Basically I wanted this to save the values on the NSUserDefaults

此外,为了从这些 textField 动态获取值,我可以这样做:

Besides, in order to dynamically obtain values from these textFields, I can do something like this:

((TextFieldCell*)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]]).textField.text

大多数情况下它都可以正常工作.但是,当 textField 由于滚动而位于视图之外时,我从 textField.text 获得的值是 (null).再次进入视野,一切又恢复正常.

It works ok most of the times. However when the textField is outside of the view because it has scrolled, the vaulue I get from textField.text is (null). As soon as it gets in the view again, everything goes back to normal.

我试图将出口从弱变强但无济于事.

I tried to change the outlet from weak to strong but to no avail.

我想我可以在类上定义一些私有 NSStrings,并在委托协议被调用时填写它们.问题是我想让我的代码尽可能通用,尽可能减少对私有变量的需求,主要是为了简化单元格生成代码.

I guess I could define some private NSStrings on the class, and fill them out when the delegate protocol gets called. The thing is that I wanted to get my code as generic as possible, keeping the need for private variables as low as possible, mostly to simplify the cell generation code.

当 textFields 在视图之外时,还有其他方法可以获取它们的值吗?

Is there any other way to get the values of the textFields when they are outside of the view?

提前致谢!

推荐答案

正如 Thyraz 所说,UITableView 只保留可见矩形的单元格 —— 以及一个允许滚动的合理缓冲区.这就是为什么重用标识符"如此重要的原因,它们指示哪些单元格可用于哪些表格(当您需要担心多个表格时至关重要).不幸的是,这本身并不能回答您的问题.

As Thyraz said, the UITableView only keeps cells for the visible rect -- and a reasonable buffer to allow for scrolling. Thats why 'reuse identifiers' are so very important, they indicate which cells can be used for which tables (critical when you have more than one table to worry about). Unfortunately, that doesn't answer your question by itself.

存储这些 textView 的内容的责任不在 UITableView 的肩上.通过数据源委托协议提供数据是的工作,因此您应该查询数据源以获取该信息.

The responsibility for storing the contents of those textViews isn't on the UITableView's shoulders. It's your job to provide that data through the data source delegate protocols, and therefore you should be querying the data source for that information.

这意味着是的,您应该将此数据存储在其他地方,通常以包含表视图的视图控制器类上的属性的形式.我建议为此目的使用 NSArray,但您也可以通过 dicts 或什至在 最后 手段(这在理论上您可以这样做,但它是非常糟糕的想法),一系列属性.就我个人而言,我几乎总是使用 NSArrays,因为它们以适合问题的方式构建,但理论上您可以通过其他方式来实现.(我只使用过一次基于 dict 的结构,那是我的数据以递归结构嵌套在自身内部的情况)

Which means that yes, you should be storing this data somewhere else, usually in the form of properties on the view controller class that contains the table view. I'd recommend the use of NSArray for the purpose, but you can also do it through dicts or even, at the last resort (and this is more a in theory you can do this, but it's an incredibly bad idea kind of thing), a series of properties. Personally, I almost always use NSArrays because they're structured in a manner appropriate to the problem, but you could theoretically do it other ways. (I've used a dict based structure exactly once, and that was a situation where my data was nested inside itself in a recursive structure)

这篇关于从 TableViewCell 上的 UITextField 获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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