tabBarController不会触发cellForRowAtIndexPath UITableViewController

时间:2016-04-08 21:36:19

标签: ios objective-c tableview uitabbaritem tabbarcontroller

我的app中有tabBarController。当点击另一个是UITableViewController的tabBar时,视图为空,并且cellForRowAtIndexPath被激活(numberOfRowsInSection不为零或为零)。

代码是:

- (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隐藏了另一个视图推送到tabBarController视图。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:3)

UITabBarController是容器视图控制器。它管理许多ViewControllers的外观。

您展示的代码是创建BookmarkCategoryViewController的新实例。这个新实例不是标签栏控制器中的实例,这就是为什么你的第一个代码块似乎没有任何效果;它没有修改屏幕视图控制器。

您的第二个代码块会推送新的视图控制器,因此您可以看到效果,但它会被推到标签栏控制器的顶部。

您需要做的是访问标签栏控制器中已有的BookmarkCategoryViewController;您可以使用选项卡栏控制器的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];
        });

    });

}