numberOfRowsInSection导致无法识别的选择器崩溃

问题描述 投票:0回答:3

我在视图控制器中有一个UITableView设置,用于填充函数返回的JSON数据。加载MatchCenterViewController时,应用程序崩溃,并且收到以下错误:

2014-06-07 15:56:23.651 Parse+Storyboard[6848:607] -[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0xaa29730
2014-06-07 15:56:23.825 Parse+Storyboard[6848:607] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0xaa29730'
*** First throw call stack:
(
    0   CoreFoundation                      0x02a8c1e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x0264a8e5 objc_exception_throw + 44
    2   CoreFoundation                      0x02b29243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x02a7c50b ___forwarding___ + 1019
    4   CoreFoundation                      0x02a7c0ee _CF_forwarding_prep_0 + 14
    5   UIKit                               0x0156f94c -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 2510
    6   UIKit                               0x0157323d -[UITableViewRowData numberOfRows] + 98
    7   UIKit                               0x013f0df2 -[UITableView noteNumberOfRowsChanged] + 120
    8   UIKit                               0x013f07a5 -[UITableView reloadData] + 814
    9   UIKit                               0x013f43b3 -[UITableView _reloadDataIfNeeded] + 65
    10  UIKit                               0x013f95f4 -[UITableView layoutSubviews] + 36
    11  UIKit                               0x01379964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
    12  libobjc.A.dylib                     0x0265c82b -[NSObject performSelector:withObject:] + 70
    13  QuartzCore                          0x0064f45a -[CALayer layoutSublayers] + 148
    14  QuartzCore                          0x00643244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    15  QuartzCore                          0x006430b0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
    16  QuartzCore                          0x005a97fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
    17  QuartzCore                          0x005aab85 _ZN2CA11Transaction6commitEv + 393
    18  QuartzCore                          0x006685b0 +[CATransaction flush] + 52
    19  UIKit                               0x013089bb _UIApplicationHandleEventQueue + 13095
    20  CoreFoundation                      0x02a1577f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    21  CoreFoundation                      0x02a1510b __CFRunLoopDoSources0 + 235
    22  CoreFoundation                      0x02a321ae __CFRunLoopRun + 910
    23  CoreFoundation                      0x02a319d3 CFRunLoopRunSpecific + 467
    24  CoreFoundation                      0x02a317eb CFRunLoopRunInMode + 123
    25  GraphicsServices                    0x02ce95ee GSEventRunModal + 192
    26  GraphicsServices                    0x02ce942b GSEventRun + 104
    27  UIKit                               0x0130af9b UIApplicationMain + 1225
    28  Parse+Storyboard                    0x00002cbd main + 141
    29  libdyld.dylib                       0x038e56d9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我已经检查了错误代码所指的UIView tableView:numberOfRowsInSection,但我看不出是什么原因造成的,因为它只是告诉它有3行。代码和屏幕截图如下。

MatchCenterViewController.m:

#import "MatchCenterViewController.h"
#import <UIKit/UIKit.h>

@interface MatchCenterViewController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *matchCenter;
@end

@implementation MatchCenterViewController



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}






- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSDictionary *matchCenterDictionary= [self.matchCenterArray objectAtIndex:indexPath.row];

    cell.textLabel.text = [matchCenterDictionary objectForKey:@"Title"];// title of the first object

    //    if([matchCenterDictionary objectForKey:@"Price"] != NULL)
    //    {
    //        cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@",[matchCenterDictionary   objectForKey:@"Price"]];
    //    }

    return cell;


}





- (void)viewDidLoad
{

    [super viewDidLoad];

self.matchCenterArray = [[NSArray alloc] init];

    //perform search with criteria just submitted
    [PFCloud callFunctionInBackground:@"MatchCenterTest"
                       withParameters:@{
                                        @"test": @"Hi",
                                        }
                                block:^(NSDictionary *result, NSError *error) {




                                    if (!error) {
                                        self.matchCenterArray = [result objectForKey:@"Top 3"];



                                        dispatch_async(dispatch_get_main_queue(), ^{
                                            [_matchCenter reloadData];
                                        });



                                        NSLog(@"Test Result: '%@'", result);

                                    }
                                }];


}

- (void)viewDidAppear:(BOOL)animated
{


    [PFCloud callFunctionInBackground:@"MatchCenterTest"
                       withParameters:@{
                                        @"test": @"Hi",
                                        }
                                block:^(NSDictionary *result, NSError *error) {

                                    if (!error) {
                                         self.matchCenterArray = [result objectForKey:@"Top 3"];


                                        dispatch_async(dispatch_get_main_queue(), ^{
                                            [_matchCenter reloadData];
                                        });


                                        NSLog(@"Test Result: '%@'", result);
                                    }
                                }];



}




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9MMnRiZi5wbmcifQ==” alt =“在此处输入图像描述”>

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9BWUJndy5wbmcifQ==” alt =“在此处输入图像描述”>

ios objective-c uitableview unrecognized-selector
3个回答
5
投票

要在界面构建器中将tableView的UITableViewDelegateUITableViewDataSource设置为UIView,而不是MatchCenterViewController

[UIView没有实现UITableViewDataSource,这就是为什么您收到无法识别的选择器错误。


0
投票

您需要将delegatedataSource设置为实现委托方法的类。您的情况就是您的MatchCenterViewController

[另外我注意到,您同时在PFCloudviewDidLoad上都调用了viewDidAppear方法。您只应在一个位置执行此操作,如果要使其执行一个,则将其添加到viewDidLoad,如果要使其在每次显示view时执行,请将其添加到viewDidAppear

还有另一件事:您应该按从上到下的顺序(在文件中)具有方法:

  1. dealloc(如果有的话)
  2. 初始化方法(initinitWithFrame等)
  3. 其他方法,相关方法归为一组,通常以#pragma mark分隔。

0
投票

您需要将MatchCenterViewController类设置为dataSource并为matchCenter表视图委托。

在initWithNibName函数中添加以下两行代码

[matchCenter setDelegate:self];[matchCenter setDataSource:self];

© www.soinside.com 2019 - 2024. All rights reserved.