在iOS中使用JSON数据创建动态表单Objective-C
问题描述:
我需要创建一个关于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": ""
},
答
你需要比这更多,但这里是一个开始,让你在正确的方向。
一个非常简单的方法可能如下所示:
在视图控制器添加一个数组来跟踪你的输入字段。
@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
}
答
- 首先你需要确定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
。 – Mukesh
我设计了窗体,但是有问题从所有字段获取数据。 任何帮助将不胜感激。 – Mukesh
我想你的意思是获取用户输入的数据?在那种情况下,你如何*想要*检索数据?有很多可能性,所以第一步是了解用户输入数据后要处理的数据。一旦你知道了,你可以(例如)创建一个字典,在你创建它时存储每个字段。从那里你可以在用户完成后再次访问它。 – LGP