pickerview的代码如何将解析后的url响应显示在pickerview中?

时间:2012-03-03 06:04:11

标签: iphone objective-c ios5 uipickerview

我已在UIPickerView中实现了以下显示响应数据的代码。它在我运行应用程序时崩溃了。如何解决问题?

- (void)viewDidLoad {
    [super viewDidLoad];


    self.view.frame=CGRectMake(0, 0, 320, 480);
    materialarray = [[NSMutableArray alloc] init];



    [picbutton setTitle:[materialarray objectAtIndex:0] forState:UIControlStateNormal];
    [self testing];
        }

-(IBAction)picbuttonaction:(id)sender{

    NSLog(@"unitpiker");
    [enterVal1 resignFirstResponder];
    [enterVal2 resignFirstResponder];


    str = picbutton.currentTitle;   
    UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:nil 
                                                      delegate:self
                                             cancelButtonTitle:@"Done"
                                        destructiveButtonTitle:nil
                                             otherButtonTitles:nil];


    Picker = [[UIPickerView alloc] init];
    Picker.tag = 1;
    Picker.showsSelectionIndicator = YES;
    Picker.dataSource = self;
    Picker.delegate = self;

    [Picker  selectRow:j inComponent:0 animated:YES];
    [menu addSubview:Picker];
    [menu showInView:self.view];   
    [menu sendSubviewToBack:Picker];     
    [menu setBounds:CGRectMake(0,0,320, 300)];

    CGRect pickerRect = Picker.bounds;
    Picker.bounds = pickerRect;

    [Picker release];
    [menu release];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    if (pickerView.tag==1) {
        return 1;
    }
    if(pickerView.tag==2){
        return 1;
    }
    return 0;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView.tag==1){ 

        return [materialarray count];
    }
    if(pickerView.tag==2){
        return [array_preferences count];
    }
    return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    if (pickerView.tag==1) {

        return [materialarray objectAtIndex:row];
    }
    if(pickerView.tag==2){
        return [array_preferences objectAtIndex:row];
    }
    return 0;
}   
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
{
    j=row;

    [picbutton setTitle:[materialarray objectAtIndex:row] forState:UIControlStateNormal];   
    }       

-(IBAction)Resetbuttonaction{

    [picbutton setTitle:[materialarray objectAtIndex:0] forState:UIControlStateNormal];
    j=0;
}


-(void)testing{
    NSString *jobSearchUrlString = [NSString stringWithFormat:@"http://infra2appsmobile.cloudapp.net/Infra2Apps.svc/Contacts"];
    //NSLog(@"url for new articles is = %@",jobSearchUrlString);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:jobSearchUrlString]];   

    NSURLConnection *theconnection =  [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (theconnection) {
        RoutData = [[NSMutableData alloc] init];
    }

}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{   

    [RoutData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [RoutData appendData:data]; 

    NSString *thexml = [[NSString alloc] initWithData:RoutData encoding:NSUTF8StringEncoding];

    NSDictionary *dictXML= [XMLReader dictionaryForXMLString:thexml error:nil];

    NSMutableArray *arrStation = [[dictXML objectForKey:@"feed"] objectForKey:@"entry"] ;//this would   



    for (int i=0; i<[arrStation count]; i++){
        str=[[[[[arrStation objectAtIndex:i] objectForKey:@"content"]   objectForKey:@"m:properties"] objectForKey:@"d:ContactName"] objectForKey:@"text"];
        //  NSlog(@"name is : %@",str);

        [materialarray addObject:str];
        [Picker reloadAllComponents];
        NSLog(@"name is->>>>>>> : %@",materialarray);        
    }

    /


}

当我运行应用程序时,它会显示:

2012-03-03 12:09:55.665 AdditionNumbers[1698:f803] Could not load the "dropdown2.png" image referenced from a nib in the bundle with identifier "com.yourcompany.AdditionNumbers"
2012-03-03 12:09:55.909 AdditionNumbers[1698:f803] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(0x14d1052 0x11bcd0a 0x14bddb8 0x2f0f 0x43064e 0x2abc 0x3689d6 0x3698a6 0x378743 0x3791f8 0x36caa9 0x1bc7fa9 0x14a51c5 0x140a022 0x140890a 0x1407db4 0x1407ccb 0x3692a7 0x36aa9b 0x2a2d 0x29a5)
terminate called throwing an exceptionCurrent  

我得到的响应如下所示我必须将这些数据显示在pickerview中它是如何实现的?

name is->>>>>>> : (
    "\n        Enrico Gaetani",
    "\n        J\U00e9r\U00f4me Tredan"
)
2012-03-03 12:01:27.865 UIPickerView[1654:f803] name is->>>>>>> : (
    "\n        Enrico Gaetani",
    "\n        J\U00e9r\U00f4me Tredan",
    "\n        Mike Tsalidis"
)
2012-03-03 12:01:27.865 UIPickerView[1654:f803] name is->>>>>>> : (
    "\n        Enrico Gaetani",
    "\n        J\U00e9r\U00f4me Tredan",
    "\n        Mike Tsalidis",
    "\n        Dana Sugarman"
)
2012-03-03 12:01:27.866 UIPickerView[1654:f803] name is->>>>>>> : (
    "\n        Enrico Gaetani",
    "\n        J\U00e9r\U00f4me Tredan",
    "\n        Mike Tsalidis",
    "\n        Dana Sugarman",
    "\n        Necip Ozyucel"
)
2012-03-03 12:01:27.867 UIPickerView[1654:f803] name is->>>>>>> : (
    "\n        Enrico Gaetani",
    "\n        J\U00e9r\U00f4me Tredan",
    "\n        Mike Tsalidis",
    "\n        Dana Sugarman",
    "\n        Necip Ozyucel",
    "\n        Goksel Topbas"
)
2012-03-03 12:01:27.868 UIPickerView[1654:f803] name is->>>>>>> : (
    "\n        Enrico Gaetani",
    "\n        J\U00e9r\U00f4me Tredan",
    "\n        Mike Tsalidis",
    "\n        Dana Sugarman",
    "\n        Necip Ozyucel",
    "\n        Goksel Topbas",
    "\n        Nizar Abdul-Baki"
)
2012-03-03 12:01:27.951 UIPickerView[1654:f803] name is->>>>>>> : (
    "\n        Enrico Gaetani",
    "\n        J\U00e9r\U00f4me Tredan",
    "\n        Mike Tsalidis",
    "\n        Dana Sugarman",
    "\n        Necip Ozyucel",
    "\n        Goksel Topbas",
    "\n        Nizar Abdul-Baki",
    "\n        Johannes Kanis"
)

1 个答案:

答案 0 :(得分:0)

您是否在.h文件中设置了UIPickerView的代理?

当您致电[materialarray]

时,viewDidLoad似乎没有数据