【问题标题】:UITableView inside UITabBarController is not workingUITabBarController 内的 UITableView 不起作用
【发布时间】:2015-11-09 21:41:00
【问题描述】:

当我想将UITableView(专为设置而设计)添加到我的UITabBarController 时,我无法使其正常工作。 UITableView 是动态原型,它在 Storyboard 上呈现,但在模拟器中它只是一个灰色方块。所以我认为这是因为我没有将 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 &lt;UITableViewDataSource, UITableViewDelegate&gt; 并将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) 

【问题讨论】:

  • 你能提供崩溃报告吗?看来我你没有正确添加tableview delegate
  • 嗨@casillas 我在问题中添加了错误信息。

标签: ios objective-c iphone uitableview uitabbarcontroller


【解决方案1】:

这是我以前在 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;

希望对你有帮助,

利亚姆

【讨论】:

    猜你喜欢
    • 2015-07-23
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 2010-11-25
    • 2017-05-17
    • 1970-01-01
    相关资源
    最近更新 更多