Loading data in Component 2 after selecting component 1 picker View IOS

夙愿已清 提交于 2019-12-13 23:51:15

问题


There are Three components (projects, tasks and sub tasks )in my pickerview when I select project, I was able to get the project name and respective project ID (Project ID is in label). My requirement is I want to send the project ID to NSURL so that I can load the respective tasks that are assigned to that project ID. Here is my Below Code. 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]];
}

Thanks in Advance Kiran Kumar


回答1:


You need to differentiate between your total data, and your displayed data.

So, download all of your data and save it in projectNames, taskNames and subtaskNames. But also have 2 other properties: currentTaskNames and currentSubtaskNames (you don't need cProjectNames because the user can always see all project names.

After the download:

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

Now, when the user selects a project, filter the tasks and subtasks that are available.

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

You need to fill in the predicates which filter out the tasks and subtasks that aren't appropriate based on the selected project and task.


Also, stop using labels for data storage...



来源:https://stackoverflow.com/questions/22093713/loading-data-in-component-2-after-selecting-component-1-picker-view-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!