从另一个选项卡重新加载UITableView

时间:2012-12-19 13:59:34

标签: iphone objective-c uitableview ios5

我无法重新加载从XML源加载的UITableView单元格数据。

以下是该方案。应用程序包含选项卡,其中一个有一个tableview,它从XML文件中获取数据并且工作正常,但事情是当我想要更改提要类别并从另一个选项卡更改XML时我可以刷新当前的tableview 。 要在标签之间切换,我使用

self.tabBarController.selectedIndex = 1;

并将类别Feed传递给我要加载的其他标签

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

它仍会加载相同的旧Feed,而不是已经传递的新Feed。我检查了NSLog并且Feed值正确传递,但切换后它不会加载。

我还尝试了当前标签和类别标签中的[self.tableView reloadData];,但它也不起作用。

3 个答案:

答案 0 :(得分:4)

您可以使用NSNotifications从其他标签发送通知,并在您的tableview中设置一个响应该通知的标记。

实施例

(调用tableview的重新加载的Tab)在你想重新加载数据时放置这个代码,所以当按下按钮或下载完成时等。

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

在UITableViewController /具有UITableView的类中,执行以下操作。

在viewDidLoad中添加:

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

然后添加函数updateTableView

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

希望这有帮助

答案 1 :(得分:1)

Ophychius建议使用通知是正确的。我假设您在XML加载完成后拥有表视图更新的所有数据源。这也假设您正在使用动态单元格。在加载XML的类中,在新XML加载完成后发布通知。

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

在表视图类中,注册为您从XML类发布的通知的观察者。

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

如您所见,这会在收到此通知时调用选择器。可以在构建表的地方调用方法,也可以创建另一个简单的方法来调用reloadData。

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

    [self.tableView reloadData];
}

最后(正如Leonardo在下面指出的那样),在viewDidUnload(或dealloc for ios6)方法中,删除该类作为该通知的观察者。

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

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

答案 2 :(得分:0)

我只是猜测,没有看到剩下的代码。

我认为您的表视图有一个NSArray数据源,您是否确保您的数组数据源也已更新?您的xml解析器或控制器是否将这些数据传输到NSArray?

因为如果你调用reloadData,它只会重新获取相同的数组。如果没有更新,您将获得旧数据。

相关问题