选择组件1选取器后,在组件2中加载数据查看IOS

时间:2014-02-28 11:19:24

标签: ios loading uipickerview

当我选择项目时,我的pickerview中有三个组件(项目,任务和子任务),我能够获得项目名称和相应的项目ID(项目ID在标签中)。我的要求是我想将项目ID发送到NSURL,以便我可以加载分配给该项目ID的相应任务。这是我的下面的代码。 viewDidLoad中:

// Code for Tasks loading
    NSString *nsTaskurllocal = @"http://test.com/";
    NSString *usrid = @"313";
    NSString * productIdString =[NSString stringWithFormat:@"%@/%@",[self.lblProjects text],usrid];
    NSLog(@"aString : %@", productIdString);
    NSString *aString = [nsTaskurllocal stringByAppendingString:productIdString];


    NSURL *nstaskurl = [NSURL URLWithString:aString];
    NSLog(@"nstaskurl : %@", nstaskurl);

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

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


    NSError *nstaskerror = [[NSError alloc] init];
    NSHTTPURLResponse *nstaskresponse = nil;
    NSData *nstaskurlData=[NSURLConnection sendSynchronousRequest:nstaskrequest returningResponse:&nstaskresponse error:&nstaskerror];
    NSURLRequest *nstaskurlRequest = [NSURLRequest requestWithURL:nstaskurl
                                                cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                            timeoutInterval:30];


    // Make synchronous request
    nstaskurlData = [NSURLConnection sendSynchronousRequest:nstaskurlRequest
                                    returningResponse:&nstaskresponse
                                                error:&nstaskerror];
    if ([nstaskresponse statusCode] >= 200 && [nstaskresponse statusCode] < 300)
    {
        NSString *nstaskresponseData = [NSJSONSerialization JSONObjectWithData:nstaskurlData
                                                                 options:NSJSONReadingAllowFragments error:&nstaskerror];

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

            for (NSDictionary *nstaskentry in nstaskentries) {
                 taskID = [nstaskentries valueForKey:@"ID_TASK"];
                taskNames = [nstaskentries valueForKey:@"TASk_NAME"];

                //NSLog(@"Error : %@", taskNames); //log to see the result in console // by Kiran
            }

            _projectpicker.delegate = self;
            _projectpicker.dataSource = self;
        }

    }       else {

    }
    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    NSNumber *myProjectArrayString = [projID objectAtIndex:row];
    //NSNumber *myTaskArrayString = [taskID objectAtIndex:row];
    //NSLog(@"%@",myArrayString);
    //NSLog(@"%@",myTaskArrayString);
    lblProjects.text = [NSString stringWithFormat:@"%@",myProjectArrayString];
    //lblProjects.hidden = YES;
    lblTasks.text = [taskNames objectAtIndex:[pickerView selectedRowInComponent:1]];
    //lblTasks.text = [NSString stringWithFormat:@"%@", myTaskArrayString];
    lblSubTasks.text = [subtaskNames objectAtIndex:[pickerView selectedRowInComponent:2]];
}

先谢谢 基兰库马尔

1 个答案:

答案 0 :(得分:0)

您需要区分数据和显示的数据。

因此,请下载所有数据并将其保存在projectNamestaskNamessubtaskNames中。但是还有另外两个属性:currentTaskNamescurrentSubtaskNames(你不需要 cProjectNames因为用户始终可以看到所有项目名称。

下载后:

self.currentTaskNames = taskNames;
self.currentSubtaskNames = subtaskNames;

现在,当用户选择项目时,过滤可用的任务和子任务。

switch (component) {
    case 0:
    {
        NSString *project = [projectNames objectAtIndex:row];
        self.currentTaskNames = [taskNames filteredArrayUsingPredicate:...];
        break;
    }
    case 1:
    {
        NSString *task = [taskNames objectAtIndex:row];
        self.currentSubtaskNames = [subtaskNames filteredArrayUsingPredicate:...];
        break;
    }
    case 2:
    // do something interesting
    break;
}

您需要根据所选项目和任务填写过滤掉不合适的任务和子任务的谓词。


另外,停止使用标签进行数据存储......