gpt4 book ai didi

ios - 从 UITableViewCell 内的 UI 元素收集数据

转载 作者:行者123 更新时间:2023-11-30 13:27:33 25 4
gpt4 key购买 nike

我正在使用具有许多不同类型的 UITableViewCellUITableView 构建表单,这些单元格可能有 UITextViewUISwitch UISliderUITextField

我需要将所有用户的输入收集到字典中,以便我可以将用户输入发送回服务器。

收集数据的最佳方法是将所有 UI 元素的委托(delegate)设置给 Controller 吗?

所以对于UITextView,在

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

sampleTextView.delegate = self
sampleTextView.tag = [tagNumber]

然后获取文本并在 -textViewDidEndEditing 中添加/更新字典:

注意 - 此外,表单是动态构建的并且可以更改。

最佳答案

在你的 UIViewController

var formInfo:NSMutableDictionary = [:]

在 cellForRowAtIndexPath 方法中将 indexPath 设置为标签

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

//... config cell

cell.sampleTextView.delegate = self
cell.sampleTextView.tag = indexPath.row

//because you are use UITableView in dynamic mode textView will lost text in scrolling ,
if formInfo.objectForKey(textview.tag) != nil {
cell.sampleTextView.text = formInfo.objectForKey(textview.tag) as? String
}

}

并在您的 textView 委托(delegate)中:

func textViewDidChange(textView: UITextView) {

formInfo.setObject(textView.text, forKey: textView.tag)

}

关于ios - 从 UITableViewCell 内的 UI 元素收集数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36984114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com