ios - UITabBarController 内的 UITableView 不工作

标签 ios objective-c iphone uitableview uitabbarcontroller

当我想添加UITableView时(专为设置而设计)进入我的UITabBarController我无法让它正常工作。 UITableView是动态原型(prototype),它在 Storyboard上渲染,但在模拟器中它只是一个灰色方 block 。所以我认为这是因为我没有将 DataSource 和 Delegate 添加到 UITableView 中。我在网上搜索了一下,有些建议这样做

@interface SecondViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

但是我的SecondViewControllerUITabBarController ,如果我在导航到此选项卡时进行此更改,应用程序将崩溃。所以就我而言,我想知道如何将 DataSource 和 Delegate 添加到 UITableView ,或任何替代方案。谢谢。

SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

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

@end

如果我实现@interface SecondViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>并将 Delegate 和 DataSource 添加到我的 SecondViewController.m 中,就像上面 -(void) viewDidLoad 中的这些内容一样

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

应用程序将崩溃并显示以下信息

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) 

最佳答案

这是我用来在 TabBarController 中包含 TableView 的代码:

#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;

希望对您有所帮助,

利亚姆

关于ios - UITabBarController 内的 UITableView 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33618816/

相关文章:

objective-c - 如何实现socket超时?

ios - 滑动删除作品,但删除按钮没有

ios - 如何自定义 PayPal Screen?

ios - ios 发布版本中的 FCM InvalidRegistration 错误

ios - 由于“NSInvalidArgumentException”而终止应用程序

ios - 多个 Storyboard未加载本地化

ios - 发送 NSMutableURLRequest 时如何在 objective-c 中使用代理服务器?

android - 状态更改后我的变量重置

iphone - EventKit 框架中的贪睡方法?

iphone - 适用于 iPad 的限制应用程序