gpt4 book ai didi

objective-c - 在 iOS 中使用 JSON 创建动态表单

转载 作者:行者123 更新时间:2023-12-04 18:10:40 26 4
gpt4 key购买 nike

我需要使用 JSON 创建动态表单。我已经解析了 JSON,但我不知道如何动态创建表单。请推荐一些代码或教程。

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

最佳答案

首先,您需要将 JSON 解析为字典,然后使用每个字典条目实例化自定义对象:

@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

一旦您在 NSArray 中拥有这些对象,例如 View Controller 中的 self.formItems,您将使用该列表来绑定(bind)您的 tableView。在 cellForRowAtIndexPath: 中,您需要将项目拉出:

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

此时您将想要动态创建 UITextField 或您需要的任何其他控件并将它们添加到表格单元格:

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

您可以将这些东西抽象到您的 FormItem 类中,但这是快速的方法。

关于objective-c - 在 iOS 中使用 JSON 创建动态表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13472001/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com