【问题标题】:Getting the value textfield from Form based UITableView [closed]从基于表单的 UITableView 获取值文本字段 [关闭]
【发布时间】:2016-03-21 07:30:13
【问题描述】:

我得到了带有一个自定义单元格的 UITableView,它将显示来自服务器响应 json 的问题数据,所以,我得到了 1 个 UILabel、3 个 UIButtons(这将是单选按钮)和 1 个 UITextField,我这样声明。

每个表格单元格的以下文本字段是备注,单选按钮(是,否,N/A)只是 UIButtons。

如您所见,这些是调查问题,请在 UITableView 中设置。

如果服务器有 21 个问题响应,我将显示 21 行,显示问题标题、选择按钮和每个表格单元格上的备注。

所以,我的问题是什么,当用户单击提交按钮时,我真的不知道如何从每个表格单元格中获取数据。

当我提交时,我想得到这样的数据: [[字符串:字符串],[字符串:字符串],...]

示例:假设我在第一个和第二个表格单元格上进行调查,我想获取这样的数据。如果用户没有填写,您将看到第三个是空数据。

[
     ["QuestionID": 1,"Type":"Yes","Remark":"Nothing special everything good"],
     ["QuestionID": 2,"Type":"No","Remark":"Yeah,it will be alot helpful"],
     ["QuestionID": 3,"Type":"","Remark":""]
] 

任何帮助?请?如何获取数据并像我们提交调查时用户没有填写一样保存。

【问题讨论】:

    标签: swift uitableview


    【解决方案1】:

    您可以通过获取NSIndexPath 来识别特定的UITextFieldUIButton。您只需要addTarget 并获取当前的NSIndexPath,然后您就可以从那里获取当前点击的UITableViewCell

    cellForRowAtIndexPath 中的第一个 addTargetUITextField

    cell.textFieldSearch.addTarget(self, action: "textChange:", forControlEvents: UIControlEvents.EditingChanged) 
    

    然后你可以在下面的函数中处理点击事件:

    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!   
    }
    

    更新(UIButton):

    cell.yesButton.addTarget(self, action: "searchByRating:", forControlEvents: UIControlEvents.TouchUpInside)
    

    自定义方法:

    func searchByRating(sender: UIButton){
    
        let cellIndexPath = tableViewSearch.indexPathForCell(sender.superview!.superview! as! SearchRatingCell)!
        let cell = tableViewSearch.cellForRowAtIndexPath(cellIndexPath!) as! SearchRatingCell
        cell.yesButton.selected = true
    }
    

    如果您有任何困惑,请告诉我。

    【讨论】:

    • 对于UIButton,你也可以做同样的事情。只需应用目标并制作自己的自定义方法。 @6245Htarwara
    • @6245Htarwara 检查编辑后的答案!
    • 介意与示例分享我吗?我仍然不知道如何从用户类型的特定 uitableviewcell 的文本字段中获取数据。我完全迷失在那里。请帮助
    • @6245Htarwara 让我检查一下!
    • @6245Htarwara 或者您能否将演示项目发给我,以便我进行编辑并发送给您。
    猜你喜欢
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多