无法从Web服务检索数据到UITableView

时间:2014-11-10 17:04:45

标签: ios uitableview

我无法从Web服务检索数据并在我的UITableView单元格中显示相同的数据。我能够看到过去添加的数据。当我转到我的应用程序时,添加一条我无法看到的记录。这是下面的代码。为了确保我使用IOS 8,Xcode 6.我最近在这个应用程序上工作并使用在IOS 7或IOS 6中正常工作。当我升级时,几乎没有问题,比如没有在UITabelView中显示数据

-(void)loadprojects
{

    NSString *eventDate = self.StringfromTextField3;
    [[NSUserDefaults standardUserDefaults] setObject:eventDate forKey:@"Eventdate"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSString *post =[[NSString alloc] initWithFormat:@"username=%@",[self.tableView dataSource]];
    NSString *strwebsite = [[NSUserDefaults standardUserDefaults] valueForKey:@"website"];
    NSString *websitemethods = @"Timesheet.svc/GetTimesheet";
    NSString *projecturltemp = [strwebsite stringByAppendingString:websitemethods];
    NSString *str = [[NSUserDefaults standardUserDefaults] valueForKey:@"UserLoginIdSession"];
    NSString *usrid = str;
    NSString * projecturl =[NSString stringWithFormat:@"%@/%@/%@",projecturltemp,usrid,eventDate];
    NSURL *url = [NSURL URLWithString:projecturl];

    //NSLog(@"url : %@", url);
    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) {
                projectNames = [entries valueForKey:@"NM_PROJECT"];
                taskNames = [entries valueForKey:@"TASk_NAME"];
                subtaskNames = [entries valueForKey:@"SUBTASK_NAME"];
                timesheetid = [entries valueForKey:@"ID_TIMESHEET_DTLS"];
                projId = [entries valueForKey:@"ID_PROJECT"];
                taskId = [entries valueForKey:@"ID_TASK"];
                subtaskId = [entries valueForKey:@"ID_SUB_TASK"];
                totalhours = [entries valueForKey:@"No_Hours"];
                approve = [entries valueForKey:@"FL_APPROVE"];
                leaves = [entries valueForKey:@"NM_LEAVE"];
            }
        }

    }       else {

    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [projectNames count];

}

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identitifier = @"Cell";
    TableViewCell * cell = [tableView
                                 dequeueReusableCellWithIdentifier:identitifier
                                 forIndexPath:indexPath];
    if ([[[timesheetid objectAtIndex:indexPath.row] stringValue] isEqualToString:@"0"])
    {
        cell.lblProjects.text = [NSString stringWithFormat:@"%@",@"No Projects Filled"];
        cell.lblTasks.text = [NSString stringWithFormat:@"%@",@"No Tasks Filled"];
        cell.lblSubTasks.text = [NSString stringWithFormat:@"%@",@"No SubTasks Filled"];
        cell.lblHours.text = [NSString stringWithFormat:@"%@",@"0 Hours Filled"];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    else if ([[leaves objectAtIndex:indexPath.row] isEqualToString:@"0"])
    {
        cell.lblProjects.text = [projectNames objectAtIndex:indexPath.row];
        cell.lblTasks.text = [taskNames objectAtIndex:indexPath.row];
        cell.lblSubTasks.text = [subtaskNames objectAtIndex:indexPath.row];
        cell.lblHours.text = [[totalhours objectAtIndex:indexPath.row] stringValue];
        NSLog(@"cell :%@",cell.lblHours.text);
        NSLog(@"lblProjects :%@",cell.lblProjects.text);
    }
    else
    {
        cell.lblProjects.text = [leaves objectAtIndex:indexPath.row];
        cell.lblTasks.text = [NSString stringWithFormat:@"%@",@"No Projects/Tasks on this Date"];
        cell.lblSubTasks.text = [NSString stringWithFormat:@"%@",@"No Sub Tasks on this Date"];
        cell.lblHours.text = [[totalhours objectAtIndex:indexPath.row] stringValue];
    }
    cell.editingAccessoryType =  UITableViewCellAccessoryDetailDisclosureButton;
    NSNumber *myProjectArrayString = [timesheetid objectAtIndex:indexPath.row];
    cell.hdbrowcount = [NSString stringWithFormat:@"%@",myProjectArrayString];

    return cell;


}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    //NSLog(@"index :%@",[timesheetid objectAtIndex:[self.tableView indexPathForSelectedRow].row]);

    [self performSegueWithIdentifier:@"segueName" sender:indexPath];
    //[tableView setEditing:YES animated:YES];
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line

    if ([[segue identifier] isEqualToString:@"segueNameforAddPage"])
    {
        UINavigationController *segueNameNavController=segue.destinationViewController;
        AddProjectDetails *NavWeekScreen = (AddProjectDetails*)segueNameNavController.topViewController;

        NSString *str = [[NSUserDefaults standardUserDefaults] valueForKey:@"UserLoginIdSession"];
        NavWeekScreen.projidstocancel = self.StringfromTextField3;
        NavWeekScreen.StringfromProjectsTasksscreenuser=str;
        NSInteger appoved = [[approve valueForKeyPath:@"@sum.integerValue"] intValue];
        NavWeekScreen.ApprovalorNot = appoved;
        NSInteger sumArray = [[totalhours valueForKeyPath:@"@sum.integerValue"] floatValue];
        NavWeekScreen.totalhrsAdd = sumArray;
    }


}

0 个答案:

没有答案