Search code examples
iosobjective-ctableviewuitabbaritemtabbarcontroller

tabBarController don't fire cellForRowAtIndexPath UITableViewController


I have tabBarController in my app. when tap another tabBar that is UITableViewController the view is empty and cellForRowAtIndexPath dosen't fire(numberOfRowsInSection not zero or nil).

Code is:

- (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;
}
}

but same code when change to:

- (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;
}
}

work correctly! but tabBarController hide and another view push to tabBarController view. Thanks for help.


Solution

  • A UITabBarController is a container view controller. It manages the appearance of a number of ViewControllers.

    The code you have shown is creating a new instance of BookmarkCategoryViewController. This new instance isn't the one that is in your tab bar controller, which is why your first block of code doesn't seem to have any effect; it is not modifying the on-screen view controller.

    Your second block of code pushes the new view controller, so you see the effect, but it is pushed over the top of your tab bar controller.

    What you need to do is access the BookmarkCategoryViewController that is already in the tab bar controller; You can do this using the viewControllers property of the tab bar controller:

    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];
            });
    
        });
    
    }