在UIPickerView中的单个组件中附加两个组件

时间:2014-03-13 11:02:46

标签: ios append uipickerview

我从昨天开始就遇到了一些问题。我在数据库中有两个表,一个是项目,另一个是表。我想在Single PickerView Component中添加两个表数据。例如,项目有pro1,proj2,proj3和Benefits表有益处1,好处2。所以我想在一个PickerView组件中附加项目表dat和Benefits表数据。

-(void)loadprojects
    {

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

        // Code for Project loading
        NSString * BenefitString =@"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 {

        }
    }

3 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

enter image description here

根据我的理解,你想拥有像上面这样的东西吗?我对么?那么UIPickerView中有多个组件?

如果是这样,首先,您需要将委托设置为您的ViewController - &gt;

<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...
    }
}

答案 2 :(得分:0)

不清楚将两个组件合并为一个是什么意思。

但我想你希望将项目和收益结合起来作为Project:Benefit

如果是这样,您可以使用以下代码段

    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);

它为你提供结果

结合:(     &#34; PROJECT1:Benefits1&#34 ;,     &#34; Project2中:Benefits2&#34; )

然后使用arrCombined作为日期选择器数据源