Stark Shaw
Stark Shaw

Reputation: 21

UITableView inside UITabBarController is not working

When I want to add a UITableView (designed for Settings) into my UITabBarController I cannot make it working properly. The UITableView is dynamic prototypes, it rendered on Storyboard but in the Simulator it is just a gray square. So I assume that is because I did not add DataSource and Delegate to the UITableView. I searched online, some suggested to do

@interface SecondViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

But my SecondViewController is a UITabBarController, somehow if I did this change when I navigate to this tab the application will be crashed. So in my case I want to know how can I add DataSource and Delegate to the UITableView, or any alternatives. Thanks.

SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property (strong, nonatomic) IBOutlet UITableView *settingsGroup;

@end

If I implement @interface SecondViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> and add Delegate and DataSource into my SecondViewController.m, like these above within -(void) viewDidLoad

self.settingsGroup.delegate = self;
self.settingsGroup.dataSource = self;

The application will crash with these information below

2015-11-10 10:42:35.557 Hard Choices[54393:2061407] -[SecondViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fe573dcd0e0

2015-11-10 10:42:35.564 Hard Choices[54393:2061407] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SecondViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fe573dcd0e0'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010f218f45 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010ec92deb objc_exception_throw + 48
2   CoreFoundation                      0x000000010f22156d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3   CoreFoundation                      0x000000010f16ed07 ___forwarding___ + 487
4   CoreFoundation                      0x000000010f16ea98 _CF_forwarding_prep_0 + 120
5   UIKit                               0x000000010f916428 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 2773
6   UIKit                               0x000000010f91a878 -[UITableViewRowData numberOfRows] + 97
7   UIKit                               0x000000010f70158a -[UITableView noteNumberOfRowsChanged] + 138
8   UIKit                               0x000000010f700bce -[UITableView reloadData] + 2069
9   UIKit                               0x000000010f70b7c7 -[UITableView layoutSubviews] + 34
10  UIKit                               0x000000010f679e40 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
11  QuartzCore                          0x000000011312759a -[CALayer layoutSublayers] + 146
12  QuartzCore                          0x000000011311be70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
13  UIKit                               0x000000010f669aa1 -[UIView(Hierarchy) layoutBelowIfNeeded] + 1083
14  UIKit                               0x000000010f7afa9c -[UITabBarController _layoutViewController:] + 877
15  UIKit                               0x000000010f7afbf0 -[UITabBarController _wrapperViewForViewController:] + 270
16  UIKit                               0x000000010f7bb08a -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 644
17  UIKit                               0x000000010f7b9f70 -[UITabBarController transitionFromViewController:toViewController:] + 59
18  UIKit                               0x000000010f7b5d81 -[UITabBarController _setSelectedViewController:] + 377
19  UIKit                               0x000000010f7b5bf7 -[UITabBarController setSelectedViewController:] + 234
20  UIKit                               0x000000010f7b9de1 -[UITabBarController _tabBarItemClicked:] + 481
21  UIKit                               0x000000010f5c6e91 -[UIApplication sendAction:to:from:forEvent:] + 92
22  UIKit                               0x000000010f993256 -[UITabBar _sendAction:withEvent:] + 498
23  UIKit                               0x000000010f5c6e91 -[UIApplication sendAction:to:from:forEvent:] + 92
24  UIKit                               0x000000010f7324d8 -[UIControl sendAction:to:forEvent:] + 67
25  UIKit                               0x000000010f7327a4 -[UIControl _sendActionsForEvents:withEvent:] + 311
26  UIKit                               0x000000010f99832e -[UITabBar(Static) _buttonUp:] + 103
27  UIKit                               0x000000010f5c6e91 -[UIApplication sendAction:to:from:forEvent:] + 92
28  UIKit                               0x000000010f7324d8 -[UIControl sendAction:to:forEvent:] + 67
29  UIKit                               0x000000010f7327a4 -[UIControl _sendActionsForEvents:withEvent:] + 311
30  UIKit                               0x000000010f7318d4 -[UIControl touchesEnded:withEvent:] + 601
31  UIKit                               0x000000010f634ed1 -[UIWindow _sendTouchesForEvent:] + 835
32  UIKit                               0x000000010f635c06 -[UIWindow sendEvent:] + 865
33  UIKit                               0x000000010f5e52fa -[UIApplication sendEvent:] + 263
34  UIKit                               0x000000010f5bfabf _UIApplicationHandleEventQueue + 6844
35  CoreFoundation                      0x000000010f145011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
36  CoreFoundation                      0x000000010f13af3c __CFRunLoopDoSources0 + 556
37  CoreFoundation                      0x000000010f13a3f3 __CFRunLoopRun + 867
38  CoreFoundation                      0x000000010f139e08 CFRunLoopRunSpecific + 488
39  GraphicsServices                    0x00000001129b4ad2 GSEventRunModal + 161
40  UIKit                               0x000000010f5c530d UIApplicationMain + 171
41  Hard Choices                        0x000000010e78cb6f main + 111
42  libdyld.dylib                       0x000000011193c92d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Upvotes: 0

Views: 186

Answers (1)

Liam Ferris
Liam Ferris

Reputation: 1846

This is the code I used to have a TableView inside a TabBarController:

#import "ViewControllerOne.h";
#import "TableViewController.h"; 

   UITabBarController *tabBarController = [[UITabBarController alloc] init];
        UIViewController *viewControllerOne = [[ViewControllerOne alloc] initWithNibName:@"ViewControllerOne" bundle:nil];
        UITableViewController *viewControllerTwo = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:nil];
        UINavigationController *myNavigationController = [[UINavigationController alloc] initWithRootViewController:viewControllerTwo];

        NSArray* controllers = [NSArray arrayWithObjects:viewController, myNavigationController, nil];
        tabBarController.viewControllers = controllers;
        self.window.rootViewController = tabBarController;

Hope this helps,

Liam

Upvotes: 1

Related Questions