按下 UITabBarItem 时重新加载 UITableView 中的数据
我有一个 UITabBarController 作为我的应用程序委托的一部分,我想在用户触摸特定选项卡(收藏夹)时捕获并强制其中的表重新加载数据。
在这种情况下,最佳实践是什么?
我已将 UITabBarDelegate 协议添加到我的应用程序委托中并实现了 didSelectViewController 方法。到目前为止,一切都很好。在该方法中,我获得了一个 viewController,因此我可以检查其标题等以确定选择了哪个选项卡。
然后如何将 reloadData 消息发送到 viewController 中的 UITableView?我尝试在我的 FavouritesViewController 类中创建一个方法并调用它,但它不起作用。
示例代码:
#pragma mark UITabBarController delegate methods
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
// If the favourites tab is pressed then reload the data
if (viewController.title = @"Favourites")
{
if ([viewController respondsToSelector:@selector(reloadFavourites:)])
{
[viewController reloadFavourites];
}
}
}
I have a UITabBarController as part of my app delegate and I want to trap when the user touches a specific tab (the favourites) and force the table within it to reload the data.
What would be best practice in this instance?
I have added the UITabBarDelegate protocol to my app delegate and implemented the didSelectViewController method. So far so good. Within the method I get a viewController, so I can check its title, etc. to determine which tab is selected.
How can I then send a reloadData message to the UITableView in the viewController? I tried creating a method in my FavouritesViewController class and calling that but it does not work.
Example code:
#pragma mark UITabBarController delegate methods
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
// If the favourites tab is pressed then reload the data
if (viewController.title = @"Favourites")
{
if ([viewController respondsToSelector:@selector(reloadFavourites:)])
{
[viewController reloadFavourites];
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您需要将
[UITableView reloadData]
添加到 tableViewController 的viewWillDisplay
方法中。这将导致表格在每次显示时重新加载。如果您想在表格视图已经显示的情况下强制重新加载,那么从您在OP中创建的方法调用重新加载应该可以。
It sounds like you need to add a
[UITableView reloadData]
to the tableViewController'sviewWillDisplay
method. This will cause the table to reload every time it is displayed.If you want to force a reload will the tableview is already displayed, then calling reload from the method you created in the OP should work.
下面的代码应该每次视图出现时刷新您的表。
The following piece of code should refresh your table every single time the view appears.