user1190239
user1190239

Reputation: 41

Create dynamic form using JSON in iOS

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"
    }
]

Upvotes: 3

Views: 2707

Answers (1)

shawnwall
shawnwall

Reputation: 4607

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.

Upvotes: 1

Related Questions