0

I have a table view on InquiryViewController that I want to be reloaded when I click the update status button. The update status button is a JSON that gets the status of a message.

Here's the code:

-(BOOL)getMessageStatus : (NSMutableArray *)emails : (UIAlertView *)alert : (UIProgressView *)progress : (InquiryLogViewController *)controller
{
    _emailsInApi = [[NSArray alloc]init];
    _emailsInApi = [[DBManager getSharedInstance]arrayOfEmails];

    NSMutableArray *newArrayEmail = [[NSMutableArray alloc]init];
    NSString *arrayEmail =@"";
    for(int i = 0; i<[_emailsInApi count]; i++){

        arrayEmail = [_emailsInApi objectAtIndex:i];

        [newArrayEmail addObject:arrayEmail];
    }

    NSURL *url = [NSURL URLWithString:@"http://www.url.com/API/2.0/message.php"];

    NSDictionary *params =@{@"api_key":@"APIKEYIOS",@"action":@"get-message-status",@"email":newArrayEmail};
    NSLog(@"params: %@",params);
    NSData *paramsData = [NSJSONSerialization dataWithJSONObject:params
                                                         options:NSJSONWritingPrettyPrinted
                                                           error:nil];

    NSString *paramsString = [[NSString alloc]initWithData:paramsData encoding:NSUTF8StringEncoding];
    paramsString = [paramsString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    paramsString = [paramsString stringByReplacingOccurrencesOfString:@"%5C" withString:@""];

    NSString *post = [NSString stringWithFormat:@"params=%@", paramsString];
    NSData *sendData = [post dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:sendData];
    NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request
                                                                 completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)

                                  {
                                      if (!error)
                                      {
                                          NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                          NSLog(@"response...... = %@", responseString );
                                          //get data frm response
                                          self.responseData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                                          self.responseArray = [self.responseData objectForKey:@"data"];


                                              for(NSDictionary *items in self.responseArray)
                                              {
                                                  Inquiry *inq = [[Inquiry alloc]init];
                                                  inq.email = [items objectForKey:@"to_email"];
                                                  inq.status = [items objectForKey:@"status_id"];

                                                  [emails addObject:inq];
                                              }

                                          float newProg = (float)1 - (float)[emails count] / (float)5;
                                          dispatch_async(dispatch_get_main_queue(), ^{
                                              [progress setProgress:newProg animated:YES];
                                          });
                                          dispatch_async(dispatch_get_main_queue(), ^{
                                              [[DBManager getSharedInstance]updateInquirylog:emails];

                                          });
                                          dispatch_async(dispatch_get_main_queue(), ^{
                                              controller.arrayOfEmails = [[DBManager getSharedInstance]loadInquiries];
                                              [controller.tableViews reloadData];

                                          });
                                          dispatch_async(dispatch_get_main_queue(), ^{
                                              [alert dismissWithClickedButtonIndex:0 animated:YES];

                                          });
                                      }
                                      else
                                      {
                                          self.alert = [[UIAlertView alloc] initWithTitle:nil message:error.localizedDescription delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
                                          self.responseData = nil;
                                          [alert show];
                                      }
                                  }];

    [task resume];
    return  YES;
}

My problem here is in: [controller.tableViews reloadData];

It doesn't reload my table. Why? Help me please. Thank you

EDIT: This json is in a class

Ok I'm sorry. I forgot to include my table view method.

Here it is:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellID =@"Cell";

    tableCellOfInquiryLog *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (tableView == self.searchDisplayController.searchResultsTableView){
        if(cell == nil)
        {
            NSArray *topLevelObjects = [[NSBundle mainBundle]loadNibNamed:@"searchCell" owner:self options:nil];

            cell = [topLevelObjects objectAtIndex:0];
        }
        Inquiry *inquiry = [searchResult objectAtIndex:indexPath.row];

        cell.sInquiry_id.text =[NSString stringWithFormat:@"%d",inquiry.ID];
        cell.sName.text = inquiry.name;
        cell.sItem.text = inquiry.item_name;
        cell.sDate.text = inquiry.dDate;
        cell.status.layer.cornerRadius = 10;

        if ([inquiry.status  isEqual: @"1"]) {
            cell.status.backgroundColor = [UIColor greenColor];
        }
        else if ([inquiry.status  isEqual: @"2"]) {
            cell.status.backgroundColor = [UIColor orangeColor];
        }
        else if ([inquiry.status  isEqual: @"3"]) {
            cell.status.backgroundColor = [UIColor redColor];
        }
        return cell;
    }
    else
    {
        if(cell == nil)
        {
            cell = [[tableCellOfInquiryLog alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        Inquiry *inquiry = [arrayOfInquiries objectAtIndex:indexPath.row];

        cell.inquiry_id.text =[NSString stringWithFormat:@"%d",inquiry.ID];
        cell.name.text = inquiry.name;
        cell.item.text = inquiry.item_name;
        cell.date.text = inquiry.dDate;
        cell.status.layer.cornerRadius = 10;

        if ([inquiry.status  isEqual: @"1"]) {
            cell.status.backgroundColor = [UIColor greenColor];
        }
        else if ([inquiry.status  isEqual: @"2"]) {
            cell.status.backgroundColor = [UIColor orangeColor];
        }
        else if ([inquiry.status  isEqual: @"3"]) {
            cell.status.backgroundColor = [UIColor redColor];
        }
        return cell;
    }
    return nil;
}
teach
  • 137
  • 11
  • Seems that your issue is explained there: http://stackoverflow.com/questions/17490286/does-dispatch-asyncdispatch-get-main-queue-wait-until-done – Larme Aug 20 '14 at 11:02

2 Answers2

0

create a instance of your tableview and the add the given code in which ever method u want to add to [self.nameoftableview reloaddata]

Nex Mishra
  • 774
  • 7
  • 13
  • it's in there: [controller.tableViews reloadData]; in the async – teach Aug 20 '14 at 11:09
  • why dont u make the cell nil and then alloc it again on reload try this i had some problem today only with reloading a table view but after re-alloc the cell it was solved – Nex Mishra Aug 20 '14 at 11:18
  • Oh thanks. Can you give an example please? I'm already wasted today sorry. – teach Aug 20 '14 at 11:20
0
 if (indexPath.section ==1) {
    cell = [tableView dequeueReusableCellWithIdentifier:section1identifier];
    cell = nil;//here i have made it nil
    if (!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:section1identifier];

    data *newObj = [callingObj.itemslist objectAtIndex:indexPath.row];

        UITextView *titlelabel = [[UITextView alloc]initWithFrame:CGRectMake(cell.frame.origin.x+5, cell.frame.origin.y-2, 320, 38)];
        titlelabel.textColor = [UIColor redColor];
        titlelabel.font= [UIFont fontWithName:@"Times New Roman" size:FONT_SIZE];
        titlelabel.text = newObj.title;
        titlelabel.scrollEnabled = NO;
        titlelabel.editable = NO;
        [cell.contentView addSubview:titlelabel];

    desView = [[UITextView alloc]initWithFrame:CGRectMake(cell.frame.origin.x,cell.frame.origin.y+25 , 320, i + 160)];
        desView.text = newObj.description;
        desView.editable = NO;
        desView.hidden = NO;
        [cell.contentView addSubview:desView];
        if (check == NO) {
            desView.hidden = YES;
        }
    }
}
return cell;
Nex Mishra
  • 774
  • 7
  • 13