5

Here is my reload function

- (void) reloadTable:(id)sender
{   
    NSLog(@"Reload Table");
    NSLog(@"%@",[appDelegate queryList]);
    [self.driverTable reloadData];
    [self.driverTable setNeedsLayout];
    [self.driverTable setNeedsDisplay];
}

Here is where I call it after receiving data from the webservice

if( [elementName isEqualToString:@"Response"]) {
    Response = NO;
    LookupView *lookupView = [[LookupView alloc] initWithNibName:@"LookupView" bundle:nil];
    [lookupView reloadTable:self];
}

Problem I have right now is after I do a search by pressing a find button and receive the data. The table does not refresh with the new data. If I call the same function again by pressing the find button again the table reloads. Now I know the data is there because I print the array out before I call the table reload.

Any ideas why?

Daniel Chen
  • 163
  • 2
  • 11
  • why do you need setNeedsLayout and setNeedsDisplay? UITableViews aren't in need of redraw, they handle that themselves at reload time. – CodaFi Mar 22 '12 at 22:28
  • oh, I was just researching for solutions here, some people with similar problems mentioned this so I was just trying – Daniel Chen Mar 22 '12 at 23:09
  • Delete those things method mentioned by @CodaFi .It doesn't belong to be there. – Dinesh Raja Mar 23 '12 at 06:09
  • @DanielChen You said your data is reloading second time, when you press find button.So is that reloadTable: method getting called or not in first time? If it is called, then what is console output you got for printed the array?? – Dinesh Raja Mar 23 '12 at 06:13
  • I believe it is getting called, as I am printing the list out when that method is getting called. And even the first time I click that button the list get printed with expected data list. And I removed the lines mentioned by @CodaFi – Daniel Chen Mar 23 '12 at 21:48

2 Answers2

6

I have run into the same issue. Per Apple's documentation, reloadData "should not be called in the methods that insert or delete rows." A workaround I have used is to delay the call to reloadData so that it is not immediately after updating table. Try this:

[self performSelector:@selector(delayedReloadData) withObject:nil afterDelay:0.5];

at the appropriate place with the following implementation of delayReloadData

 -(void)delayedReloadData{
    [self.tableView reloadData];
}

A negative side affect I have see from this approach is that if the table row is visible there is a short delay before it shows its content.

bobnoble
  • 5,794
  • 3
  • 25
  • 32
  • [this](http://stackoverflow.com/questions/14018754/why-reloaddata-shouldnt-be-called-on-methods-that-delete-insert-rows) question can count as a follow up to @bobnoble's answer – abbood Dec 24 '12 at 08:20
0

I found out you are doing XML parsing by seeing that elementName in your if loop. Why you need to load a NIB file in your parsing code. What is the use of it? You can just call your reloadTable: method just like this.

[self reloadTable:self];

You are populating your tableView using some array in your app delegate file. So did you initialized your delegate in your ViewController?

Dinesh Raja
  • 8,501
  • 5
  • 42
  • 81
  • Yes you are right, I am populating the array that contains the data for the table using the app delegate. Once I know all the information is loaded into the array I use the app delegate to call the refresh table function in the view controller of the table. – Daniel Chen Mar 23 '12 at 21:52