【问题标题】:UIPicker view not displaying data from JSON arrayUIPicker 视图不显示来自 JSON 数组的数据
【发布时间】:2011-09-18 15:48:24
【问题描述】:

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

请告诉我如何在单击 UI 按钮时显示 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);
}

【问题讨论】:

    标签: json nsmutablearray nsarray uipickerview


    【解决方案1】:

    我认为问题在于这行代码:

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

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

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

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

    【讨论】:

    • 当我尝试更改行并打印列表 objectAtIndex:1 时,我能够 NSLog 值。奇怪,但它没有显示在pickerView上
    • 我确实调整了代码并尝试了一些不同的东西,但仍然有效。我已经更新了我的最新代码。谁能帮忙。
    • 发现了问题.. 硬编码数组在 viewdidLoad 方法中有效,但在 connectionDidFinishLoading 方法中无效。 UIPickerview 似乎没有在那里加载。
    猜你喜欢
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多