我正在使用具有许多不同类型的UITableView
的UITableViewCell
构建表单。这些单元格可能具有UITextView
,UISwitch
,UISlider
,UITextField
等。
我需要将所有用户输入都收集到Dictionary
中,以便可以将用户输入发送回服务器。
收集数据的最佳方法是将所有UI元素的委托设置为Controller吗?
所以对于UITextView
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
sampleTextView.delegate = self
sampleTextView.tag = [tagNumber]
然后获取文本并在
- textViewDidEndEditing:
中添加/更新字典注意-此外,表单是动态构建的,可以更改。
最佳答案
在您的UIViewController中
var formInfo:NSMutableDictionary = [:]
在cellForRowAtIndexPath方法中,将indexPath设置为tag
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/