【问题标题】:Create dynamic form using JSON data in iOS Objective-C在 iOS Objective-C 中使用 JSON 数据创建动态表单
【发布时间】:2017-10-16 09:17:19
【问题描述】:

我需要创建一个关于 JSON 数据值的动态表单。我已经解析了 JSON 数据并获得了具有不同控件(如 Textfield、TextView、下拉列表和 Switch)的数据,但不知道如何在表单视图中动态添加字段。任何帮助将不胜感激。谢谢!

{
"success": true,
"result": {
"label": "Contacts",
"name": "Contacts",
"createable": true,
"updateable": true,
"deleteable": true,
"retrieveable": true,
"fields": [
  {
    "name": "salutationtype",
    "label": "Salutation",
    "mandatory": false,
    "type": {
      "name": "string"
    },
    "nullable": true,
    "editable": true,
    "default": ""
  },
  {
    "name": "firstname",
    "label": "First Name",
    "mandatory": false,
    "type": {
      "name": "string"
    },
    "nullable": true,
    "editable": true,
    "default": ""
  },
  {
    "name": "contact_no",
    "label": "Contact Id",
    "mandatory": false,
    "type": {
      "name": "string"
    },
    "nullable": false,
    "editable": false,
    "default": ""
  },
  {
    "name": "phone",
    "label": "Office Phone",
    "mandatory": false,
    "type": {
      "name": "phone"
    },
    "nullable": true,
    "editable": true,
    "default": ""
  },
  {
    "name": "lastname",
    "label": "Last Name",
    "mandatory": true,
    "type": {
      "name": "string"
    },
    "nullable": false,
    "editable": true,
    "default": ""
  },
  {
    "name": "mobile",
    "label": "Mobile Phone",
    "mandatory": false,
    "type": {
      "name": "phone"
    },
    "nullable": true,
    "editable": true,
    "default": ""
  },
  {
    "name": "account_id",
    "label": "Organization Name",
    "mandatory": false,
    "type": {
      "refersTo": [
        "Accounts"
      ],
      "name": "reference"
    },
    "nullable": true,
    "editable": true,
    "default": ""
  },
  {
    "name": "leadsource",
    "label": "Lead Source",
    "mandatory": false,
    "type": {
      "picklistValues": [
        {
          "label": "Cold Call",
          "value": "Cold Call"
        },
        {
          "label": "Existing Customer",
          "value": "Existing Customer"
        },
        {
          "label": "Self Generated",
          "value": "Self Generated"
        },
        {
          "label": "Employee",
          "value": "Employee"
        },
        {
          "label": "Partner",
          "value": "Partner"
        },
        {
          "label": "Public Relationship",
          "value": "Public Relationship"
        },
        {
          "label": "Direct Mail",
          "value": "Direct Mail"
        },
        {
          "label": "Conference",
          "value": "Conference"
        },
        {
          "label": "Trade Show",
          "value": "Trade Show"
        },
        {
          "label": "Website",
          "value": "Website"
        },
        {
          "label": "Word of Mouth",
          "value": "Word of Mouth"
        },
        {
          "label": "Others",
          "value": "Others"
        }
      ],
      "defaultValue": "Cold Call",
      "name": "picklist"
    },
    "nullable": true,
    "editable": true,
    "default": ""
  },

【问题讨论】:

    标签: ios objective-c xcode ios-autolayout


    【解决方案1】:

    您需要的远不止这些,但这是一个让您朝着正确方向前进的开始。

    非常简单的方法如下所示:

    在您的视图控制器中添加一个数组来跟踪您的输入字段。

    @interface ViewController ()
    
    @property (strong, nonatomic) NSMutableArray *fields;
    
    @end
    

    在您的视图控制器的 viewDidLoad(或其他适当的位置)中添加一个循环来创建每个元素并将其添加到您的视图控制器的视图中。

    self.fields = [NSMutableArray arrayWithCapacity:10]; // Clear and init
    
    CGFloat y = 0.0; // field position
    for (NSDictionary *field in jsonData[@"fields"]) {
        // create a label for the field
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10 + y, 100, 20)];
        label.text = field[@"label"];
        [self.view addSubview:label];
    
        // create the field
        NSString *fieldTypeName = ((NSDictionary *)field[@"type"])[@"name"];
        if ([fieldTypeName isEqualToString:@"string"]) {
            UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10 + y, 100, 20)];
            [self.view addSubview:textField];
    
            // Keep track of the fields
            [self.fields addObject:@{ @"data": textField, @"json": field } ];
        } else if ([fieldTypeName isEqualToString:@"picklist"]) {
            // create picklist and add to view
        } // add more field types here...
    
        y += 25; // move to next field position
    }
    

    当然,您还需要获取用户输入的字段数据。一种简单的方法是将每个字段添加到以后可以读取的数组中。

    要访问用户数据,您只需遍历它们。

    for (NSDictionary *field in self.fields) {
        // Your initial JSON data
        NSDictionary *jsonField = (NSDictionary *)field[@"json"];
        // Your input field
        UITextField *textField = (UITextField *)field[@"data"];
        // User data
        NSString *userData = textField.text;
        // Do something with the data
    }
    

    【讨论】:

    • 这让我想了很多。
    • 我设计了表单,但在从所有字段中获取数据时遇到问题。任何帮助将不胜感激。
    • 我想你的意思是获取用户输入的数据?在这种情况下,您想要如何检索数据?有很多可能性,所以第一步是了解用户输入数据后你想对数据做什么。一旦你知道了,你就可以(例如)创建一个字典,在创建每个字段时存储它。用户完成后,您可以从那里再次访问它。
    • 是的,我想获取所有文本字段的值。
    • 第一个问题是如何检索用户在文本字段中输入的内容,然后将每个字段存储在字典中。
    【解决方案2】:
    • 首先您需要识别 JSON 对象,以便识别您需要 UILabel、UITextfield 等...
    • 在 JSON 响应中设置所有标志,例如动态字段是否可编辑。
    • 必须是你的json数据结构修复

    其他链接: https://www.codeproject.com/Articles/637408/Updating-UITableView-with-a-dynamic-data-source

    https://useyourloaf.com/blog/dynamically-loading-new-rows-into-a-table/

    【讨论】:

    • 看清楚一个问题,这不是问题的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 2019-03-27
    • 2012-05-01
    • 2017-07-12
    • 1970-01-01
    • 2015-03-13
    • 2019-07-20
    相关资源
    最近更新 更多