从自定义表格视图单元格获取文本字段值 [英] Getting Textfield Values from Custom Table View Cell
问题描述
项目从服务器加载到动态表视图中......我需要为通过文本字段输入的每个项目发回一个数量编号.我无法为文本字段创建插座,因为它给了我错误:插座无法连接到重复的内容.
我还尝试使用标记引用文本字段 (UITextField *countTextfield = (UITextField *)[cell viewWithTag:4];
) 但这并没有获取我输入的值.>
当我单击保存计数时,我想以以下形式发回 json:
<块引用>{"InventoryID":"1231","manual_quantity":234}{"InventoryID":"232","manual_quantity":23}{"InventoryID":"214","manual_quantity":1241}{"InventoryID":"241","manual_quantity":1123241}
其中 manual_quantity 是输入到文本字段中的值
请看我链接的图片
对我来说通知表格视图单元格更改的一些方法是为表格视图单元格定义一个 protocol
以通知它们更改并在视图控制器中实现协议,视图控制器将收到这些更改的通知.
- 定义协议.
- 在视图控制器中实现
protocol
. - 在表格视图单元格中定义一个
protocol
类型的成员. - 在
UITableView cellForRowAt
或任何方便的方法中,将protocol
类型的表格视图单元格成员设置为self
(即实现协议
).
通过这种protocol
机制,表格视图单元可以将它们的实际值更改通知任何实现protocol
的类.
在使用 CellDelegate
协议的伪代码中:
在视图控制器中:
//视图控制器协议 CellDelegate {func updateQuantity(库存:字符串,数量:整数)}类 MyUIViewController: UIViewController, CellDelegate {func updateQuantity(库存:字符串,数量:整数){//更新给定库存的数量}}
在表格视图单元格中:
public class MyCell: UITableViewCell {...@IBOutlet 弱变量库存:UIText!@IBOutlet 弱变量数量:UIText!var cellDelegate:CellDelegate?//表格视图单元格@IBAction func textChanged(_ sender: Any) {//库存和数量是表格视图单元格中的出口.cellDelegate.updateQuantity(库存,数量)}
请原谅我的伪代码有点太笨了.
Items are loaded up on dynamic table view from server ... I need to send back a quantity number for each item which is entered through the textfield. I cannot create an outlet for the textfield because it gives me the error: outlets cannot be connected to repeating content.
I also tried to reference the textfield with a tag (UITextField *countTextfield = (UITextField *)[cell viewWithTag:4];
) but this does not grab the value I type in.
When I click Save Count I want to send back json in the form:
{"InventoryID":"1231","manual_quantity":234} {"InventoryID":"232","manual_quantity":23} {"InventoryID":"214","manual_quantity":1241} {"InventoryID":"241","manual_quantity":1123241}
Where manual_quantity is the value that is typed into the textfield
please see images I have linked
Something that works for me to notify table view cells changes, is to define a protocol
for the table view cells to notify of their changes and implement the protocol in a view controller, that will be notified of those changes.
- Define a protocol.
- Implement the
protocol
in a view controller. - Define in the table view cell a member of type
protocol
. - In the
UITableView cellForRowAt
or any convenient method set the table view cell member of typeprotocol
toself
(i.e. the view controller implementing theprotocol
).
With this protocol
mechanism, table view cells can notify of their changes with their actual values to any class implementing the protocol
.
In pseudo-code using a CellDelegate
protocol:
In the view controller:
// View controller
protocol CellDelegate {
func updateQuantity(inventory: String, quantity: Int)
}
class MyUIViewController: UIViewController, CellDelegate {
func updateQuantity(inventory: String, quantity: Int) {
// Update quantity for given inventory
}
}
In the table view cell:
public class MyCell: UITableViewCell {
...
@IBOutlet weak var inventory: UIText!
@IBOutlet weak var quantity: UIText!
var cellDelegate: CellDelegate?
// Table view cell
@IBAction func textChanged(_ sender: Any) {
// inventory and quantity being outlets in the table view cell.
cellDelegate.updateQuantity(inventory, quantity)
}
Apologise the pseudo code is a little bit Swiftish.
这篇关于从自定义表格视图单元格获取文本字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!