开发者

UIPicker view not displaying data from JSON array

i have a UIPicker view for which i need to load the data from a JSON feed. I am able to write the array data into NSLOG and display it , but not able to see it in the UIPickerview . please see the code below and help me

Please also let me know how we can make the UIPicker view appear when i click a UIbutton and close the UIPickerview when a value is selected .

#import "orderSamples.h"
#import "SBJson.h"
#import "JSON.h"

@implementation orderSamples

NSMutableArray *products;
NSArray *list;
NSMutableData *responseData;
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}
 */

- (void)viewDidLoad
{


    [super viewDidLoad];

    products = [[NSMutableArray alloc] init];

    responseData = [[NSMutableData data] retain];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http JSON URL"]];
    [[NSURLConnection alloc] initWithRequest:reque开发者_运维问答st delegate:self ];


}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    NSLog(@"didReceiveResponse"); 
    [responseData setLength:0]; 
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data]; 
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Connection failed: %@", [error description]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];


list = [[NSMutableArray alloc] init];


NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//[responseData release];

NSDictionary *dictionary = [responseString JSONValue];
NSArray *response = [[dictionary valueForKey:@"products"] retain];

list = [response valueForKey:@"product_name"];

NSLog(@"Here is the products list: %@",[response valueForKey:@"product_name"]);

self.pickerData = list;
[list release];    
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [list count];

}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [list objectAtIndex:row];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSString *str = [list objectAtIndex:row];
    NSLog(@"Selected : %@" , str);
}


I think the problem is this line of code:

list = [dictionary valueForKey:@"products"];

This is assigning an object to your "list" variable, but that object will be autoreleased. Instead try:

list = [[dictionary valueForKey:@"products"] retain];

And you should probably be using instance variables rather than global variables, although if you have only one network connection at a time it doesn't really matter much.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜