2014-03-13 114 views
0

從昨天開始,我就遇到了非常小的問題。我在數據庫中有兩個表,其中有Projects和其他Benefits。我想在單PickerView組件中添加這兩個表數據。例如項目有pro1,proj2,proj3和Benefits表有益處1,好處2。所以我想在一個PickerView組件中追加兩個項目table dat和Benefits表格數據。在UIPickerView中的單個組件中追加兩個組件

-(void)loadprojects 
    { 

     NSString *post =[[NSString alloc] initWithFormat:@"username=%@",[self.projectpicker dataSource]]; 

     // Code for Project loading 
     NSString * BenefitString [email protected]"http://test.com/GetBenefitTypes"; 

     NSURL *Benefiturl = [NSURL URLWithString:BenefitString]; 

     NSString *projecturltemp = @"http://test.com/GetAssignedProjects"; 
     NSString *str = [[NSUserDefaults standardUserDefaults] valueForKey:@"UserLoginIdSession"]; 
     NSString *usrid = str; 
     NSString * projecturl =[NSString stringWithFormat:@"%@/%@",projecturltemp,usrid]; 


     NSURL *url = [NSURL URLWithString:projecturl]; 

     NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 




     NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]]; 
     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
     [request setURL:Benefiturl]; 
     [request setURL:url]; 
     [request setHTTPMethod:@"POST"]; 
     [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
     [request setValue:@"application/projectpicker" forHTTPHeaderField:@"Accept"]; 
     [request setValue:@"application/jsonArray" forHTTPHeaderField:@"Content-Type"]; 
     [request setHTTPBody:postData]; 


     NSError *error = [[NSError alloc] init]; 
     NSHTTPURLResponse *response = nil; 
     NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
     NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url 
                cachePolicy:NSURLRequestReturnCacheDataElseLoad 
               timeoutInterval:30]; 
     NSURLRequest *urlRequestBenifits = [NSURLRequest requestWithURL:Benefiturl 
                cachePolicy:NSURLRequestReturnCacheDataElseLoad 
               timeoutInterval:30]; 


     // Make synchronous request 
     urlData = [NSURLConnection sendSynchronousRequest:urlRequest 
             returningResponse:&response 
                error:&error]; 
     urlData = [NSURLConnection sendSynchronousRequest:urlRequestBenifits 
                returningResponse:&response 
                   error:&error]; 
     if ([response statusCode] >= 200 && [response statusCode] < 300) 
     { 
      NSString *responseData = [NSJSONSerialization JSONObjectWithData:urlData 
                    options:NSJSONReadingAllowFragments error:&error]; 

      NSArray *entries = [NSJSONSerialization JSONObjectWithData:[responseData dataUsingEncoding:NSUTF8StringEncoding] 
                   options:0 error:&error]; 
      if(!entries) 
      { 
       NSLog(@"Error : %@", error); 
      } 
      else{ 

       for (NSDictionary *entry in entries) { 
        projID = [entries valueForKey:@"ID_PROJECT"]; 
        projectNames = [entries valueForKey:@"NM_PROJECT"]; 
        BenefitsNames = [entries valueForKey:@"NM_LEAVES"]; 
       } 
       //Combined = [BenefitsNames arrayByAddingObjectsFromArray:projectNames]; 
       NSLog(@"Combined : %@", projectNames); 
       //NSLog(@"projID : %@", projID); 
       _projectpicker.delegate = self; 
       _projectpicker.dataSource = self; 
      } 

     }  else { 

     } 
    } 
+0

爲什麼不使用2個組件?你到目前爲止嘗試了什麼?什麼沒有用? – Wain

+0

我在我的pickerview中有三個組件,我想在1個組件中添加這個配置和項目。 – user3401290

+0

我能夠加載所有組件中的所有數據,但我希望將此優點添加到組件中的項目組件1 – user3401290

回答

0

項目和好處NSArray實例?如果是這樣,爲什麼不把它們組合成一個數組,並使用前進?

NSArray *projects = @[@"pro 1", @"pro 2"]; 
    NSArray *benefits = @[@"benefit 1", @"benefit 2"]; 

    NSArray *combined = [projects arrayByAddingObjectsFromArray:benefits]; 

    NSLog(@"Combined: %@", combined); 

組合打印出來:

Combined: (
    "pro 1", 
    "pro 2", 
    "benefit 1", 
    "benefit 2") 

然後在您- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component方法只是我的理解返回[combined objectAtIndex:row];

+0

我想我們非常接近,但我不知道我哪裏出錯了,讓我編輯代碼給你 – user3401290

0

enter image description here

基地,你想有像上面?我對麼?所以在UIPickerView中有多個組件?

如果是這樣,第一次的事情,你需要代表團設爲您的ViewController - >

<UIPickerViewDataSource, UIPickerViewDelegate> 

那麼你實現你的ViewController.m文件中的以下方法: (我假設你有「的NSArray *數據「爲項目和好處準備好...)

//Number of rows to display in each component 
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    if (component==0) { 
     return [self.listOfProjectsOfLeftCol count]; 
    } 
    return [self.listOfBenefitsOfRightCol count]; 
} 

//Number of columns to display 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 2; 
} 

//define what to display in each rows and columns 
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    if (component==0) { 
     return [self.listOfProjectsLeftCol objectAtIndex:row]; 
    } 
    return [self.listOfBenefitsOfRightCol objectAtIndex:row]; 
} 

//when the row is selected , do something... 
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    if (component==0) { 

     //selected items in left column, now write your own code to do something... 

    } 

    else { 
      //selected items in right column, now write your own code to do something... 
    } 
} 
+0

沒有塞弗你誤會了我,假設我有好處組件1中的數據和組件2中的數據,我希望這兩個好處和項目數據在一個組件中 – user3401290

0

不清楚你是什麼意思將兩個組件合而爲一。

不過,我想你想的項目和效益作爲項目的組合:受益

如果是這樣ü可以使用下面的代碼片段

NSArray *arrProject=[NSArray arrayWithObjects:@"Project1",@"Project2",nil]; 
    NSArray *arrBenefits=[NSArray arrayWithObjects:@"Benefits1",@"Benefits2",nil]; 

    NSMutableArray *arrCombined=[[NSMutableArray alloc]init]; 
    for(int i=0;i<[arrProject count];i++) 
    { 
     [arrCombined addObject:[NSString stringWithFormat:@"%@:%@",[arrProject objectAtIndex:i],[arrBenefits objectAtIndex:i]]]; 
    } 
    NSLog(@"Combined:%@",arrCombined); 

它給你造成的

聯合:( 「Project1:Benefits1」, 「Project2:Benefits2」 )

And th en使用arrCombined作爲日期選取器數據源

相關問題