views:

57

answers:

3

how can I refresh the content of UITableView every 5 minutes?

A: 

Put an NSTimer in and shoot it off every 300 seconds to refresh table view?

Louie
+1  A: 

create an NSTimer that calls a method 'foo' every 5 mins (in seconds), then in 'foo' just write

[self.tableView reloadData];
jmont
I think [NSTimer timerWithTimeInterval:300 target:self selector:@selector(foo) userInfo:nil repeats:YES]; should do it.
jmont
where do I place this line? viewDidLoad?
Yolanda
Using timerWithTimerInterval... you would also need to add the timer to a run loop.
Vegar
+1  A: 

Checkout NSTimer and UITableViews reloadData.

Assuming that the view of the current controller is a UITableView, and that you have declared a NSTimer called timer in your controller, the following code should do the trick:

- (void)viewDidLoad {
   timer = [NSTimer scheduledTimerWithTimeInterval:300.0 
                                            target:[self view] 
                                          selector:@selector(reloadData) 
                                          userInfo:nil 
                                           repeats:YES];

   [super viewDidLoad];
}

UPDATE:

Someone else has proposed the use timerWithTimeInterval:target:selector:userInfo:repeats: which is basically the same as the scheduledTimerWithTimeInterval that I have used in my example.

They differ, though, in that scheduledTimerWithTimeInterval adds the timer to the current run loop, whilst with the timerWithTimerInterval method, you will need to add the timer to a run loop your self.

Vegar
If the current view controller is a `UIViewController`, you'll have to use `self.tableView`. (`self.view` will probably work, but Apple says `self.tableView` is the `UITableView`)
Douwe Maan
tableView is a property of UITableViewController, not UIViewController. UIViewControllers view property reflects whatever view the controller is told to control, and in my little test app, thats a UITableView.
Vegar