在iOS Objective-C中使用JSON数据创建动态表单

时间:2017-10-16 09:17:19

标签: ios objective-c xcode ios-autolayout

我需要创建一个关于JSON数据值的动态表单。我已经解析了JSON数据,并使用Textfield,TextView,Drop-Down List和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": ""
  },

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:-1)

  • 首先你需要识别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/