ios - tabBarController 不触发 cellForRowAtIndexPath UITableViewController

标签 ios objective-c tableview uitabbaritem tabbarcontroller

我的应用程序中有 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/

相关文章:

iphone - iOS Game Center 提交时间已过

ios - UIPageViewController 在移动到第三个 View Controller 后取消分配第一个 View Controller ?

ios - 跟踪相机的位置和旋转

iphone - 检测用户何时点击 UIDatePicker 中的选择指示器?

ios - 我可以省略 .m 文件中的委托(delegate)声明吗

ios - 使用应用组在应用之间通信和保存数据

ios - 交互式推送通知 - 隐藏/显示按钮

ios - 无法更新 tableView 中的部分

ios - 根据在不同的 TableView Controller 中单击的内容转至 TableView Controller

components - 如何将组件按行宽度均匀分布?