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

Create dynamic form using JSON data in iOS Objective-C

我需要创建一个关于 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": ""
  },
  • 首先您需要确定 JSON 对象,以便确定您需要 UILabel、UITextfield 等...
  • 在 JSON 中设置所有标志 响应类似于动态字段是否可编辑。
  • 必须是你的 json 数据结构修正

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

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

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

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

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

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