0
我有一個UIPicker視圖,爲此我需要從JSON源加載數據。 我能夠將數組數據寫入NSLOG並顯示它,但無法在UIPickerview中看到它。請參閱下面的代碼,並幫助我UIPicker視圖不顯示來自JSON數組的數據
請讓我知道我們如何使UIPicker視圖出現,當我點擊一個UIbutton並關閉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);
}
我試過了。但似乎沒有工作。 – UnlimitedMeals
當我嘗試更改行和打印列表objectAtIndex:1我能夠NSLog的值。奇怪,但它並沒有顯示在pickerView上 – UnlimitedMeals
我確實調整了代碼並嘗試了一些差異化的東西,但仍然有力。我已更新智慧我最新的代碼。任何人都可以幫忙 – UnlimitedMeals