我得到了一个自定义单元格的UITableView,它将显示来自服务器响应json的问题数据,所以,我得到了1个UILabel,3个UIButtons(这将是单选按钮)和1个UITextField,我就是这样声明的.
每个表格单元格下面的文本字段是备注,单选按钮(是,否,N/A)只是UIButtons.
如您所见,这些是调查问题,在UITableView上进行设置.
如果服务器有21个问题响应,我将显示21行,显示每个tablecell上的问题标题,选择按钮和备注.
那么,我的问题是什么,当用户点击提交按钮时,我真的不知道如何从每个表格单元格中获取数据.
当我提交时,我想得到这样的数据:[[String:String],[String:String],...]
示例:假设我对第一个和第二个表格单元格进行调查,我想获得这样的数据.如果用户没有填充,您将看到第三个是空数据.
[
["QuestionID": 1,"Type":"Yes","Remark":"Nothing special everything good"],
["QuestionID": 2,"Type":"No","Remark":"Yeah,it will be alot helpful"],
["QuestionID": 3,"Type":"","Remark":""]
]
Run Code Online (Sandbox Code Playgroud)
任何帮助?请?如何获取数据并保存,即使用户在我们提交调查时也没有填写.
您可以识别特定的UITextField
或UIButton
通过获取NSIndexPath
.你只需要addTarget
获得当前的数据,NSIndexPath
然后就可以获得当前点击的数据UITableViewCell
.
首先addTarget
对UITextField
在cellForRowAtIndexPath
:
cell.textFieldSearch.addTarget(self, action: "textChange:", forControlEvents: UIControlEvents.EditingChanged)
Run Code Online (Sandbox Code Playgroud)
然后你可以在下面的函数中处理click事件:
func textChange(textField: UITextField){
var cellIndexPath = tableViewSearch.indexPathForCell(textField.superview!.superview! as! UITableViewCell)!
let cell = tableViewSearch.cellForRowAtIndexPath(cellIndexPath!) as! SearchRatingCell
cell.textFieldSearch.text = textField.text!
}
Run Code Online (Sandbox Code Playgroud)
更新(
UIButton
):
cell.yesButton.addTarget(self, action: "searchByRating:", forControlEvents: UIControlEvents.TouchUpInside)
Run Code Online (Sandbox Code Playgroud)
自定义方法:
func searchByRating(sender: UIButton){
let cellIndexPath = tableViewSearch.indexPathForCell(sender.superview!.superview! as! SearchRatingCell)!
let cell = tableViewSearch.cellForRowAtIndexPath(cellIndexPath!) as! SearchRatingCell
cell.yesButton.selected = true
}
Run Code Online (Sandbox Code Playgroud)
如果您有任何困惑,请告诉我.
归档时间: |
|
查看次数: |
1122 次 |
最近记录: |