我的应用程序中有 tabBarController。当点击另一个 tabBar(即 UITableViewController)时, View 为空,并且 cellForRowAtIndexPath 不会触发(numberOfRowsInSection 不为零或 nil)。
代码是:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
[[viewController navigationController] popToRootViewControllerAnimated:NO];
switch (tabBarController.tabBar.selectedItem.tag) {
case 1:
NSLog(@"Home");
break;
case 2:
NSLog(@"Profile");
break;
case 3:
{
NSLog(@"Bookmark");
BookmarkCategoryViewController *bookmarkVC =[self.storyboard instantiateViewControllerWithIdentifier:@"BookmarkCategory"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
/// Background work
BookmarkManager *p = [[BookmarkManager alloc]init];
[p fetchBookmarks:self.categoryId];
bookmarkVC.entries = p.appRecordList;
bookmarkVC.categoryId = self.categoryId;
bookmarkVC.ID_list_entries = _ID_list_entries;
[bookmarkVC.tableView reloadData];
dispatch_async(dispatch_get_main_queue(), ^{
/// Update UI
[tabBarController setSelectedIndex:1];
});
});
}
break;
case 4:
NSLog(@"Setting");
break;
default:
NSLog(@"Home");
break;
}
}
但更改为相同的代码:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
[[viewController navigationController] popToRootViewControllerAnimated:NO];
switch (tabBarController.tabBar.selectedItem.tag) {
case 1:
NSLog(@"Home");
break;
case 2:
NSLog(@"Profile");
break;
case 3:
{
NSLog(@"Bookmark");
BookmarkCategoryViewController *bookmarkVC =[self.storyboard instantiateViewControllerWithIdentifier:@"BookmarkCategory"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
// Background work
BookmarkManager *p = [[BookmarkManager alloc]init];
[p fetchBookmarks:self.categoryId];
bookmarkVC.entries = p.appRecordList;
bookmarkVC.categoryId = self.categoryId;
bookmarkVC.ID_list_entries = _ID_list_entries;
[bookmarkVC.tableView reloadData];
dispatch_async(dispatch_get_main_queue(), ^{
/// Update UI
[bookmarkVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self.navigationController pushViewController:bookmarkVC animated:YES];
});
});
}
break;
case 4:
NSLog(@"Setting");
break;
default:
NSLog(@"Home");
break;
}
}
工作正常!但 tabBarController 隐藏,另一个 View 推送到 tabBarController View 。 感谢您的帮助。
最佳答案
UITabBarController
是一个容器 View Controller 。它管理许多 ViewController 的外观。
您显示的代码正在创建 BookmarkCategoryViewController
的新实例。这个新实例不是标签栏 Controller 中的实例,这就是为什么您的第一个代码块似乎没有任何效果;它不会修改屏幕 View Controller 。
您的第二个代码块推送新的 View Controller ,因此您可以看到效果,但它被推送到标签栏 Controller 的顶部。
您需要做的是访问标签栏 Controller 中已有的BookmarkCategoryViewController
;您可以使用选项卡栏 Controller 的 viewControllers
属性来执行此操作:
case 3:
{
NSLog(@"Bookmark");
BookmarkCategoryViewController *bookmarkVC = (BookmarkCategoryViewController *)tabBarController.viewControllers[tabBarController.tabBar.selectedItem];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
// Background work
BookmarkManager *p = [[BookmarkManager alloc]init];
[p fetchBookmarks:self.categoryId];
bookmarkVC.entries = p.appRecordList;
bookmarkVC.categoryId = self.categoryId;
bookmarkVC.ID_list_entries = _ID_list_entries;
dispatch_async(dispatch_get_main_queue(), ^{
[bookmarkVC.tableView reloadData];
});
});
}
关于ios - tabBarController 不触发 cellForRowAtIndexPath UITableViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36509735/