0

I want to add a refresh bar button in my view controller I put in the viewdidload() this code:

UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshTable)];
self.navigationItem.leftBarButtonItem = refreshButton;

and the refreshtable function is :

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

    NSLog(@"table is refreshing ....");
}

this is a screenshot of the refresh button: enter image description here

but it doesn't work, shall i add something else in the refreshtable function?, the table view doesn't refreshed!, the message "table is refreshing ...." appears everytime i click on the refresh button, but the table doesn't load new data!

when refreshing, can I have this icon somewhere? enter image description here

If I had two table view in my view controller, and I want when I click on the refresh button, just the first table to be reloaded,

enter image description here

I put the same you code that you suggested, but it doesn't work here! should I do something else?

Community
  • 1
  • 1
CarinaM
  • 527
  • 4
  • 12
  • 27
  • sorry, I edited just now – CarinaM Jun 08 '13 at 11:30
  • Change the log to NSLog(@"table (%@) is refreshing ....", self.tableView); and see what it says – Wain Jun 08 '13 at 11:32
  • 2
    You need to explain why do you expect something different to show up in the table on `reloadData`. Did the data source change between the time the table was shown before and the moment you tapped `[refresh]`? – Sergey Kalinichenko Jun 08 '13 at 11:33
  • @CarinaM: Where are you getting those data, that you need to display on tableview ? In which method you are fetching new data ? – Midhun MP Jun 08 '13 at 11:34
  • @MidhunMP ; layer = ; contentOffset: {0, 0}> – CarinaM Jun 08 '13 at 11:35
  • @MidhunMP the data is getting from json file – CarinaM Jun 08 '13 at 11:36
  • @dasblinkenlight this table contains data from json file, and this data is changed always, so I want when I press on the refresh button to get the new data – CarinaM Jun 08 '13 at 11:37
  • @CarinaM: Ok, then in which method you are getting the json ? – Midhun MP Jun 08 '13 at 11:37
  • @MidhunMP dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://....../fastnews.php"]]; [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES]; }); -(void)fetchedData:(NSData *)responseData{ NSError *error; NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; fastResults = [json objectForKey:@"nodes"]; [self.tableView reloadData]; } – CarinaM Jun 08 '13 at 11:39
  • @CarinaM: In which method you added this code `dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://....../fastnews.php"]]; [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES]; });` ? Is it in the `refreshTable` ? – Midhun MP Jun 08 '13 at 11:41
  • @MidhunMP in viewdidLoad(), what should I put in refreshTable()? – CarinaM Jun 08 '13 at 11:42
  • @MidhunMP wait I will try it, then give u a feedback – CarinaM Jun 08 '13 at 11:59
  • @CarinaM: Ok, all the best :) – Midhun MP Jun 08 '13 at 12:00

3 Answers3

0

try this .

[self.tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] 
             withRowAnimation:UITableViewRowAnimationNone];

[self.tableView endUpdates];
Anil
  • 151
  • 1
  • 2
  • 11
0

The issue is you added the data fetching logic in your viewDidLoad (according to your comments).

The viewDidLoad method will be called when the view is loading. It' won't be called unless you dismiss the view and present it again(it won't be calles if you call the reloadData it's just for reloading the UITableView).

When you call the reloadData the data source is still with old data, it is not updated.

So implement your refreshTable method like:

- (void) refreshTable
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
         NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://....../fastnews.php"]];
         [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
    });
}

-(void)fetchedData:(NSData *)responseData
{
   NSError *error;
   NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
  fastResults = [json objectForKey:@"nodes"];
  [self.tableView reloadData];
}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • please before you leave, do you have an idea how can I display the refresh icon (to let the user knows that the view is refreshing), check screen above – CarinaM Jun 08 '13 at 12:56
  • please can you reply on my question! – CarinaM Jun 10 '13 at 06:31
  • @CarinaM: You can use the `UIActivityIndicator` for this. – Midhun MP Jun 10 '13 at 06:40
  • can you send me a simple code how can I implement it in my above code? – CarinaM Jun 10 '13 at 07:13
  • @CarinaM: add this code `UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; spinner.center = CGPointMake(160, 240); spinner.hidesWhenStopped = YES; [self.view addSubview:spinner]; [spinner startAnimating];` as the first line of `refreshTable` method – Midhun MP Jun 10 '13 at 07:54
  • and how can I stop the animation of it? because still animated – CarinaM Jun 10 '13 at 08:13
  • thank you, it is done, I put stopanimating here: ` dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http:....."]]; [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];[spinner stopAnimating]; });` – CarinaM Jun 10 '13 at 08:24
  • hello again, please can you check above, I just edited my question please – CarinaM Jun 10 '13 at 08:36
0

You need to update your dataSourceAaaay in refreshTable method before reload table.

Prateek Prem
  • 1,544
  • 11
  • 14