Reloading TableView on iPhone from another class
So I have a FirstViewController, which is a UITableViewController, as well as delegate and dataSource. It has the table view.
I have another class, FeedParser, that parses the data - but after it's done parsing, I need it to go and refresh the UITableView or else it won't show anything.
This is probably a stupid question, so forgive me, but how should I go about calling FirstViewController's tableView.reloadData from FeedParser?
Is there a method开发者_高级运维 to return that view?
Thanks!
Register the view controller to receive notifications that the data has been changed, and have it refresh the table when it receives one. Then have the parser send it out.
Registering for it is easy:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(yourRefreshMethodHere:)
name:@"YourNotificationName"
object:nil];
Your refresh method needs to be set up to receive these notifications, along these lines:
- (void)reloadTable:(NSNotification *)notif {
[self.yourTableName reloadData];
}
And it's important to stop observing in your ViewDidUnload:
[[NSNotificationCenter defaultCenter] removeObserver:self];
Then in the parser you need to simply add this when it's complete:
[[NSNotificationCenter defaultCenter] postNotificationName:@"YourNotificationName"
object:nil];
The view controller (and anyone else observing the notification with that name) will get the message and perform its task.
Simple method,
Create a object for your FirstViewController
in appDelegate, assign property and synthesize it.
In ViewDidLoad
of FirstViewController,
firstViewControllerObj = self;
In feedParser.m
, after the parsing done code as follows,
[appDelegate.firstViewControllerObj.tabelView reloadData];
Just store a reference to FirstViewController on your app delegate, then call [appDelegate.firstViewController.tableView reloadData]
.
精彩评论