在Pickerview中加载已编辑的数据

时间:2014-03-25 07:12:24

标签: ios loading uipickerview

我有一个表格视图,其中包含已保存的数据。当我点击表格视图单元格时,它会导航到详细页面,其中包含一个包含3个组件的选择器视图,一个文本字段和一个提交按钮。我的问题是当我点击表格视图单元格时,它需要获取具有所选下拉值的数据。例如,我们有proj1,proj2,proj3,proj4,当我们点击tableview单元格时,它需要将proj3显示为选定值。这是我的代码。

    -(void)loadprojects
    {
        NSString *post =[[NSString alloc] initWithFormat:@"username=%@",[self.projectpicker dataSource]];
        //NSString *pickername = [self.projectpicker dataSource];
        //NSString *key = @"Da9s^a2Rp4na6R$ikiAsav3Is@niWsa";

        //NSString *encrypteduname = [AESCrypt encrypt:pickername password:key];

        // Code for Project loading
        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: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];


        // Make synchronous request
        urlData = [NSURLConnection sendSynchronousRequest:urlRequest
                                        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"];
                }
                randomSelection=[BenefitNames arrayByAddingObjectsFromArray:projectNames];
                randomSelectionID = [benID arrayByAddingObjectsFromArray:projID];
                //NSLog(@"Error : %@", projectNames);
                //NSLog(@"projID : %@", projID);
                _projectpicker.delegate = self;
                _projectpicker.dataSource = self;
            }

        }       else {

        }
    }

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    int numberofRows = 0;
    switch (component) {

        case 0:
            [pickerView reloadComponent:1];
            numberofRows = [randomSelection count];
            break;
        case 1:
             [pickerView reloadComponent:2];
            numberofRows = [taskNames count];

            break;
        case 2:
            numberofRows = [subtaskNames count];
            break;
    }
    return numberofRows;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *title;
    if(component == 0) {
        [pickerView reloadComponent:1];
        [pickerView reloadComponent:2];
        title = [randomSelection objectAtIndex:row];
    }
    else if (component == 1){
        [pickerView reloadComponent:2];
        title = [taskNames objectAtIndex:row];
    }
    else{
        title = [subtaskNames objectAtIndex:row];

    }
    return title;

}

    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
        //NSLog(@"%@",myArrayString);
        //NSLog(@"%@",myTaskArrayString);
        if(component == 0){
            NSNumber *myProjectArrayString = [randomSelectionID objectAtIndex:row];
            lblProjects.text = [NSString stringWithFormat:@"%@",myProjectArrayString];
            lblProjects.hidden = YES;
            lblBenefitsLeaves.text = [randomSelection objectAtIndex:[pickerView selectedRowInComponent:0]];
            hdnlblProjects.text = [randomSelection objectAtIndex:[pickerView selectedRowInComponent:0]];
             rownoedit =  row;
            [pickerView reloadComponent:1];
            [pickerView reloadComponent:2];
            [self loadtasks];

        }
        //lblProjects.hidden = YES;
        else if(component == 1)
        {
            NSNumber *myTaskArrayString = [taskID objectAtIndex:row];
            lblTasks.text = [NSString stringWithFormat:@"%@",myTaskArrayString];
            lblTasks.hidden = YES;
            hdnlblTasks.text = [taskNames objectAtIndex:[pickerView selectedRowInComponent:1]];
            [pickerView reloadComponent:2];
            [self loadsubtasks];

        }
        else if(component == 2)
        {
            NSNumber *mysubtaskArrayString = [subtskID objectAtIndex:row];
            lblSubTasks.text = [NSString stringWithFormat:@"%@",mysubtaskArrayString];
            NSLog(@"%@",lblSubTasks.text);
            lblSubTasks.hidden = YES;
            hdnlblSubTasks.text = [subtaskNames objectAtIndex:[pickerView selectedRowInComponent:2]];

            //lblTasks.text = [taskNames objectAtIndex:[pickerView selectedRowInComponent:1]];
            //lblTasks.text = [NSString stringWithFormat:@"%@", myTaskArrayString];
            //lblSubTasks.text = [subtaskNames objectAtIndex:[pickerView selectedRowInComponent:2]];
        }
    }

请帮帮我们

0 个答案:

没有答案