2013-05-12 54 views
0

iOS的新手。我正在使用AFNetworking下载JSON数据并将数据放入Dictionary对象中。这在下面的代码完成:如何使用AFNetworking使用填充了JSON数据的NSDictionary对象填充原型单元格文本标签

-(void)locationRequest 
{ 
NSURL *url = [NSURL URLWithString:@"http://sitespec/php/getlocations.php"]; 

NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
//AFNetworking asynchronous url request 
AFJSONRequestOperation *operation = [AFJSONRequestOperation 
            JSONRequestOperationWithRequest:request 
            success:^(NSURLRequest *request, NSHTTPURLResponse   *response, id responseObject) 
            { 

             self.locationsFromJSON = (NSDictionary *)responseObject; 

             NSLog(@"JSON RESULT %@", responseObject); 
             NSLog(@"JSON DICTIONARY %@", _locationsFromJSON); 
             [self.tableView reloadData]; 
            } 
            failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id responseObject) 
            { 
             NSLog(@"Request Failed: %@, %@", error, error.userInfo); 
            }]; 

[operation start]; 

} 

我得到的数据和填充字典阵列作为的NSLog的outpur看到以下称:

2013-05-12 12:44:08.851 sitespec[2024:c07] JSON RESULT (
    { 
    city = "Rocky Mount"; 
    id = 1; 
    name = "Rocky Mount"; 
    phone = "(252) 451-1811"; 
    state = NC; 
    "street_address" = "620 Health Drive"; 
    zip = 27804; 
}, 
    { 
    city = "Elizabeth City"; 
    id = 2; 
    name = "Elizabeth City"; 
    phone = "(252) 335-4355"; 
    state = NC; 
    "street_address" = "109 Corporate Drive"; 
    zip = 27906; 
} 
) 
2013-05-12 12:44:08.852 sitespec[2024:c07] JSON DICTIONARY (
    { 
    city = "Rocky Mount"; 
    id = 1; 
    name = "Rocky Mount"; 
    phone = "(252) 451-1811"; 
    state = NC; 
    "street_address" = "620 Health Drive"; 
    zip = 27804; 
}, 
    { 
    city = "Elizabeth City"; 
    id = 2; 
    name = "Elizabeth City"; 
    phone = "(252) 335-4355"; 
    state = NC; 
    "street_address" = "109 Corporate Drive"; 
    zip = 27906; 
} 
) 

,但这里是我失去了.....

如何我在

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

满足访问Dictionary对象这个数据HOD?

类似:

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

cell.textLabel.text = [self.locationsFromJSON objectForKey:@"name"]; 

???

这不起作用,我得到一个SIGBART错误。我只是不知道如何读取数据并填充表视图?

我该怎么做?任何帮助非常感谢.....

回答

1

我认为这很通常我不明白人们为什么要从字典中提取数据并用字典内容填充表视图的努力。我不明白为什么人们不为JSON响应创建模型类,然后根据他们需要将模型添加到数组中。

所以汤米我强烈建议你创建一个名为Address类模型(例如),并添加所有的JSON属性作为属性模型类,当您分析您的JSON创建Address类的实例,你将增加到一个数组。在此之后,在你的表视图类,你将有地址的阵列和:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section将返回yourAddressesArray.count

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { Address *address = [yourAddressesArray objectAtIndex:indexPath.row]; //assign your address properties to labels etc 

}

,当你有模型类很容易;)

EDIT FOR PARSING

从我看到的你的问题你有一个字典阵列。所以,我建议做这样的事情:

在地址类中创建一个IMIT法josn:

-(id)initAddressWithJSON:(id)JSON { 
    slef = [super init]; 
    if(self) { 
     self.city = [JSON objectForKey:@"city"]; 
     // set all the properties here 
    } 
    return self; 
} 

当你收到名为JSON响应,让我们说“jsonResopnse”你应该这样做:

-(NSArray*)myAddressesFromJSON:(id)JSON { 

    //you should add a validation here for JSON (not null not empty) 
    NSMutableArray *addresses = [[NSMutableArray alloc] init]; 
    Address *address; 
    for(NSDictionary *addressDict in JSON) { 
     address = [[Address alloc] initWithJSON:addressDict]; 
     [addresses addObject:address]; 
    } 
    return addresses; 
} 

注意,我从我的头写的代码,我没有用Xcode的:),并可能有一些构建错误(拼写错误),但我希望你有这个想法。另外我想你正在使用JSONKit。

+0

我创建了一个地址模型,并添加了我的JSON数据的所有属性。我已经将Address.h文件导入到我的tableView.m文件中,我在那里下载数据并希望显示tableView。我有一个名为locationsFromJSON的数组,定义如上所述。尽管如此,你说“当你解析你的JSON时,你创建了Address类实例,你将添加到数组中”我在返回的值中有JSON响应:responseObject。我如何创建Address类实例并将它们添加到数组中? – 2013-05-12 18:15:25

+0

请检查我编辑的答案 – danypata 2013-05-12 18:31:44

0

正如我所看到的,您在同一类中使用JSON请求和UITableView。在cellForRowAtIndexPath方法中设置断点并检查是否已有数据。如果不是,则:

询问- (NSInteger)numberOfRowsInSection:(NSInteger)section方法或已经在numberOfSections如果您的字典已经填满。否则返回0。

而且在收到JSON数据调用[tableView reloadData];

如果这不是问题的梅索德,请告诉你在哪里得到的SIGBART错误;)

-1

感谢danypata提供了很好的反馈意见。我采取了一个稍微不同的路线来实现这一目标。让我想到的是danypata的评论“看起来像你在你的JSON响应中有一系列词典”......在AFNetworking块中,我得到了JSON响应,我将JSON对象分配给定义为属性的数组当前TableVIewCOntroller。

-(void)locationRequest 

{ NSURL * URL = [NSURL URLWithString:@ “myurl”];

NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
//AFNetworking asynchronous url request 
AFJSONRequestOperation *operation = [AFJSONRequestOperation 
            JSONRequestOperationWithRequest:request 
            success:^(NSURLRequest *request, NSHTTPURLResponse *response, id responseObject) 
            { 

             self.locationsFromJSON = responseObject; 
             [self.tableView reloadData]; 
            } 
            failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id responseObject) 
            { 
             NSLog(@"Request Failed: %@, %@", error, error.userInfo); 
            }]; 

[operation start]; 

} 

我也声明了一个可变的字典财产和财产的NSString在我的JSON响应中的每个值。在下面的TableVIewCell方法中,我获取位于index.row处的对象字典,并读出键的值并将它们分配给原型单元格标签属性。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"locCell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
if (cell == nil) { 

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 

} 


self.dictObj = [[NSMutableDictionary alloc] init]; 
self.dictObj = [_locationsFromJSON objectAtIndex:indexPath.row]; 
cell.textLabel.text = [_dictObj valueForKey:@"city"]; 
cell.detailTextLabel.text = [_dictObj valueForKey:@"phone"];  
return cell; 
} 

这工作!它只是让别人指出明显的“看起来像你有一个字典对象数组......”