UIPicker 视图不显示 JSON 数组中的数据

发布于 2024-12-05 17:18:32 字数 2664 浏览 1 评论 0原文

我有一个 UIPicker 视图,我需要从 JSON feed 加载数据。 我能够将数组数据写入 NSLOG 并显示它,但无法在 UIPickerview 中看到它。请参阅下面的代码并帮助我,

还请让我知道如何在单击 UIbutton 时显示 UIPicker 视图,并在选择值时关闭 UIPickerview 。

#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:request 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 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:request 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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗酒趁年少 2024-12-12 17:18:32

我认为问题是这行代码:

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

这是将一个对象分配给您的“列表”变量,但该对象将被自动释放。相反,请尝试:

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

您可能应该使用实例变量而不是全局变量,尽管如果您一次只有一个网络连接,那么这并不重要。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文