3

I need to create a dynamic form using JSON. I have parsed the JSON but i don't have any idea how to create a form dynamically. Please suggest some code or tutorial.

[
    {
        "cssClass": "head"
    },
    {
        "cssClass": "input_text",
        "values": "Text Field",
        "fieldsize": "small",
        "required": "undefined",
        "prevalue": "This is text field",
        "autocaps": "none",
        "fieldesc": "text field description"
    },
    {
        "cssClass": "number",
        "values": "Number ",
        "fieldsize": "small",
        "required": "required",
        "prevalue": "This is Number Field",
        "autocaps": "capitalize",
        "fieldesc": "number field description"
    },
    {
        "cssClass": "email",
        "values": "Email",
        "fieldsize": "small",
        "required": "required",
        "prevalue": "This is email field",
        "autocaps": "none",
        "fieldesc": "email field description"
    },
    {
        "cssClass": "password",
        "values": "Password",
        "fieldsize": "small",
        "required": "required",
        "prevalue": "password",
        "autocaps": "none",
        "fieldesc": "password field description"
    },
    {
        "cssClass": "date",
        "values": "Date",
        "fieldsize": "medium",
        "required": "required",
        "prevalue": "datefield",
        "autocaps": "uppercase",
        "fieldesc": "date field description"
    },
    {
        "cssClass": "time",
        "values": "Time",
        "fieldsize": "small",
        "required": "undefined",
        "prevalue": "time field",
        "autocaps": "uppercase",
        "fieldesc": "time field description"
    }
]
2
  • create class file with all fields and store then all in NSArray and pass to table view and reload data once you get data from WS Commented Nov 20, 2012 at 11:16
  • can u give some link or sample code on this Commented Nov 20, 2012 at 12:02

1 Answer 1

1

First you will want to parse the JSON into a dictionary, and then instantiate custom objects using each of the dictionary entries:

@interface FormItem : NSObject

@property (nonatomic, retain) NSString * cssClass;
@property (nonatomic, retain) NSString * values;
//etc

+(id)initWithDictionary:(NSDictionary*)dictionary;

@end

@implementation FormItem

+(id)initWithDictionary:(NSDictionary*)dictionary {
    if (self = [super init]) {
        _cssClass = [dictionary valueForKey:@"cssClass"];
        //etc
    }
}

@end

Once you have those objects in a NSArray such as self.formItems in a view controller, you will use that list for binding your tableView. In cellForRowAtIndexPath: you will want to pull the item out:

FormItem *currentItem = self.formItems[indexPath.row];

At that point you will want to dynamically create UITextField's or whatever other controls you need and add them to the table cells:

if ([currentItem.values isEqualToString:@"Text Field"] {
    UITextField *text = [[UITextField alloc] init...];
    //setup
    [cell.contentView addSubview:text];
}

You could abstract this stuff up into your FormItem class but this is the quick approach.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.