reload tableview after call to WCF service

282 Views Asked by At

I am calling a WCF service to display data in a UITableViewController.The code in the .m file is:

- (void)viewDidLoad
{
  [super viewDidLoad];
  [docTable setDataSource:self];
  [docTable setDelegate:self];
}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    EDViPadDocSyncService *service = [[EDViPadDocSyncService alloc]init];
    EDVCategory *cat = [EDVCategory alloc];
    cat.categoryId = [catId intValue];
    [service getDocsByCatId:self action:@selector(getDocsByCatIdHandler:) category:cat];
    [docTable reloadData];
}

- (void) getDocsByCatIdHandler: (id)value 
{
if([value isKindOfClass:[NSError class]]) 
    {
    NSLog(@"%@", value);
    return;
}
if([value isKindOfClass:[SoapFault class]]) 
    {
    NSLog(@"%@", value);
    return;
    }               
    NSMutableArray* result = (NSMutableArray*)value;
    NSMutableArray *documentList = [[NSMutableArray alloc] init];
    self.myDocList = [[NSMutableArray array] init];
    for (int i = 0; i < [result count]; i++)
    {
       EDVDocument *docObj = [[EDVDocument alloc]init];
       docObj = [result objectAtIndex:i];
       [documentList addObject:[docObj docName]];        
    }
    self.myDocList = documentList;
    [docTable reloadData];
}

- (void)viewDidUnload
{  
   [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int cnt = [self.myDocList count];
    NSLog(@"ABC=%@",cnt);
    return [self.myDocList count];
    //return 1;
 }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   DocumentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DocumentCell"];
   if (cell == nil)
   {
      cell = [[[DocumentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DocumentCell"] autorelease];
   }
   NSLog(@"cell text=%@",[self.myDocList objectAtIndex:indexPath.row]);
   cell.lblDocName.text = [self.myDocList objectAtIndex:indexPath.row];
   return cell;
}

I am using storyboard.I have hooked the "docTable",set the datasource and the delegate for "docTable".The problem is,the service is called after the call to "numberOfRowsInSection".So,'return [self.myDocList count]' is 0.I have put [docTable reloadData] in viewWillAppear as well as in the service handler,that is,"getDocsByCatIdHandler".But it isn't getting reloaded,as expected.Is there anything else I can try? EDIT:- This a Master-Detail application.I have used the same code for loading data in the "MasterViewController" UITableViewController and it works.When the user selects a cell in this table,I need to populate data in the second tableview by calling the WCF service.The second tableview isn't displaying data.

2

There are 2 best solutions below

7
On

Everything looks good which leads me to believe you are not getting the results from the webservice you are expecting.

One small thing first thats not your problem. If result is in fact an array and there is an object in it, you shouldnt need to alloc a new EDVDocument.

EDVDocument *docObj = [result objectAtIndex:i];

Can you log the (id)value parameter to see what we're working with?

NSLog(@"%@", value);

If value is not an array, the cast wont complain, it will just work by not working. However, if it is an array you may be finding trouble with assigning your property (granted I dont know how its declared) to a local array. You can use the following function to create a new array with the elements of your temporary array;

self.myDocList = [[NSArray alloc] initWithArray:documentList];
[docTable reloadData];

I hope this helps.

0
On

I was facing the same issue when i have a async webService call. I was using a private Library to call webservice so my control goes to the library and after the response comes a method in Appdelegate is set as handler. So what you need to do is before calling the Webservice save the state of tableview in a shared variable and after you have received response set it back to tableView and then call the reload method. Something like below:

SharedView.tblView = self.tableView; 
[webservice Call];

After Response:
 self.tableView = SharedView.tblView;
[self.tableView reloadData];

Hope This Helps.