2017-10-16 129 views
-1

我需要創建一個關於JSON數據值的動態表單。我已經解析了JSON數據,並獲得了不同控件(如Textfield,TextView,下拉列表和開關)的數據,但不知道如何在窗體視圖中動態添加字段。任何幫助將非常感激。謝謝!在iOS中使用JSON數據創建動態表單Objective-C

{ 
"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": "" 
    }, 

回答

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 
} 
+0

。 – Mukesh

+0

我設計了窗體,但是有問題從所有字段獲取數據。 任何幫助將不勝感激。 – Mukesh

+0

我想你的意思是獲取用戶輸入的數據?在那種情況下,你如何*想要*檢索數據?有很多可能性,所以第一步是瞭解用戶輸入數據後要處理的數據。一旦你知道了,你可以(例如)創建一個字典,在你創建它時存儲每個字段。從那裏你可以在用戶完成後再次訪問它。 – LGP

-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/

+0

讀一個問題顯然,這不是一個問題的答案給了我很多東西值得思考的問題 – karthikeyan

相關問題