Reload UITableView from another tab

China☆狼群 提交于 2020-01-04 04:10:14

问题


I am having trouble trying to reload UITableView cell data which are being loaded from an XML source.

Here is the scenario. App contains tabs, in one of them there is a tableview which gets it's data from an XML file and works just ok, but the thing is when I want to change the feed category and change the XML from another tab I can refresh the current tableview. For switching between tabs I use

self.tabBarController.selectedIndex = 1;

and pass the category feed to the other tab which I want to load

xmlParser = [[XMLParser alloc] loadXMLByURL:categories];

and it still loads the same old feed, not the new one which has been passed. I checked with NSLog and the feed value passes properly but it just wont load after switching.

I also tried to [self.tableView reloadData]; from both current tab and the category tab and it didn't work either.


回答1:


You can use NSNotifications to send a notification from your other tab and have a oberver in your tableview that responds to that notification.

Example

(Tab calling the reload of the tableview) put this code whenever you want to reload the data, so when a button is pushed or a download is finished etc.

NSNotification * notification = [NSNotification notificationWithName:@"updateTable" object:nil];
[[NSNotificationCenter defaultCenter] postNotification:notification];

In the UITableViewController / the class with the UITableView, do the following.

in your viewDidLoad add:

[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(updateTableView) name:@"backtolist" object:nil];

Then add the function updateTableView

- (void) updateTableView: (NSNotification*) note
{
    //Do whatever needs to be done to load new data before reloading the tableview
    [_tableView reloadData];
}

Hope this helps




回答2:


Ophychius was correct in his suggestion to use Notifications. I'm assuming you have all of the data sources for your table view updating when the XML is finished loading. This also assumes you're using dynamic cells. In the class that loads the XML, post a Notification when the new XML is finished loading.

    [[NSNotificationCenter defaultCenter] postNotificationName:@"XMLLoaded" object:nil];

In the table view class, register as an observer for the Notification you posted from the XML class.

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTable:) name:@"XMLLoaded" object:nil];

As you can see, this calls a selector when this notification is received. Either call your method where you build the table, or create another simple method to call reloadData from.

-(void)reloadTable:(NSNotification *)notif
{
    NSLog(@"In ReloadTable method. Recieved notification: %@", notif);

    [self.tableView reloadData];
}

Finally (as Leonardo pointed out below), in your viewDidUnload (or dealloc for ios6) method, remove the class as an observer of that notification.

- (void)viewDidUnload
{
    [super viewDidUnload];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



回答3:


I am just guessing, without having seen the rest of the code.

I suppose your table view has an NSArray datasource, did you make sure that your array datasource is updated too ? Does your xml parser, or controller, transfer those data to the NSArray ?

Because if you call reloadData it is just going to refetch the same array. And if it is not updated, you would get old data.



来源:https://stackoverflow.com/questions/13954003/reload-uitableview-from-another-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!