尝试调用tableView.reloadData时是否存在NSInvalidArgumentException?

时间:2016-06-28 03:47:21

标签: ios objective-c uitableview

我看到了所有重复的问题并尝试了所有解决方案。但我无法成功运行我的应用程序。

单击按钮,我从Database类获取NSMutableArray并将其存储在本地NSMutableArray对象(我为tableView设置)中,并调用tableView.reloadData。

这是我的代码,

#import "SearchPage.h"
#import "DBManager.h"

@interface SearchPage ()

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *testData;
@property (nonatomic,strong) UIButton *button;
-(IBAction)buttonpressed:(id)sender;
@end

@implementation SearchPage


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];
    }
    cell.textLabel.text=[_testData objectAtIndex:indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.testData.count;

}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.testData=[[NSMutableArray alloc]init];
 /*  [self.testData addObject:@"Clash of clans"];
    [self.testData addObject:@"Prince of persia"];
    [self.testData addObject:@"Vice city"];
    [self.testData addObject:@"San andreas"];
    [self.testData addObject:@"Tomb rider"];
    [self.testData addObject:@"Counter Strike"];  */

    self.screenRect=[[UIScreen mainScreen]bounds];
    self.width=self.screenRect.size.width;
    self.height=self.screenRect.size.height;

    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 60, _width, _height-60) style:UITableViewStylePlain];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.view addSubview:self.tableView];

    self.textField = [[UITextField alloc] initWithFrame: CGRectMake(70.0f, 80.0f, 100.0f , 30.0f)];
    self.textField.borderStyle = UITextBorderStyleRoundedRect;
    self.textField.returnKeyType = UIReturnKeyNext;
    self.textField.delegate = self;
    [self.view addSubview:self.textField];

    self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.button.frame = CGRectMake(200.0f , 80.0f , 100.0f, 30.0f);
    [self.button addTarget:self action:@selector(buttonpressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.button setTitle:@"Find it" forState:UIControlStateNormal];
    [self.view addSubview:self.button];

    self.label = [[UILabel alloc]initWithFrame:CGRectMake(70, 110.0f, 240.0f , 30.0f)];
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.text = @"Textbox should not be empty";
    self.label.textColor = [UIColor redColor];

    [self.view addSubview:self.label];

}

- (IBAction)buttonpressed:(id)sender {
    [self findData];
    [[self tableView] reloadData];
}


-(void) findData {
 //   [self.testData removeAllObjects];
    self.testData = [NSMutableArray arrayWithArray:[[DBManager getSharedInstance] getAll]];
  //  self.testData = [[DBManager getSharedInstance] getAll];

    for(int i=0;i<self.testData.count;i++){
        NSLog(@"FROM ARRAY : %d",[[self.testData objectAtIndex:i] intValue]);
    }


}



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

按下按钮是按下按钮时调用的功能。

我试过了,

[self.testData removeAllObjects];

也尝试了这个,

self.testData = [NSMutableArray arrayWithArray:[[DBManager getSharedInstance] getAll]];

而不是这个 self.testData = [[DBManager getSharedInstance] getAll];

没有任何效果。

当它到达reloadData行时,应用程序崩溃并发出以下错误。

-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0xb000000000000092
2016-06-28 09:20:07.756 CalcwithDBProgramatically[528:8810] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0xb000000000000092'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010519fe65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000104c16deb objc_exception_throw + 48
    2   CoreFoundation                      0x00000001051a848d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00000001050f590a ___forwarding___ + 970
    4   CoreFoundation                      0x00000001050f54b8 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x0000000105c10a19 -[UITableViewLabel setText:] + 119
    6   CalcwithDBProgramatically           0x00000001045f50c5 -[SearchPage tableView:cellForRowAtIndexPath:] + 341
    7   UIKit                               0x00000001058cfe43 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 766
    8   UIKit                               0x00000001058cff7b -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
    9   UIKit                               0x00000001058a4a39 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2996
    10  UIKit                               0x00000001058d901c -[UITableView _performWithCachedTraitCollection:] + 92
    11  UIKit                               0x00000001058bfedc -[UITableView layoutSubviews] + 224
    12  UIKit                               0x000000010582d4a3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
    13  QuartzCore                          0x0000000109d2259a -[CALayer layoutSublayers] + 146
    14  QuartzCore                          0x0000000109d16e70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
    15  QuartzCore                          0x0000000109d16cee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    16  QuartzCore                          0x0000000109d0b475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
    17  QuartzCore                          0x0000000109d38c0a _ZN2CA11Transaction6commitEv + 486
    18  UIKit                               0x0000000105770f7c _UIApplicationHandleEventQueue + 7329
    19  CoreFoundation                      0x00000001050cba31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    20  CoreFoundation                      0x00000001050c195c __CFRunLoopDoSources0 + 556
    21  CoreFoundation                      0x00000001050c0e13 __CFRunLoopRun + 867
    22  CoreFoundation                      0x00000001050c0828 CFRunLoopRunSpecific + 488
    23  GraphicsServices                    0x00000001097d5ad2 GSEventRunModal + 161
    24  UIKit                               0x0000000105776610 UIApplicationMain + 171
    25  CalcwithDBProgramatically           0x00000001045f916f main + 111
    26  libdyld.dylib                       0x0000000106dc092d start + 1
    27  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

感谢您的时间(:

2 个答案:

答案 0 :(得分:0)

cell.textLabel.text=[_testData objectAtIndex:indexPath.row];

而不是上面的行,请尝试如下:

NSNumber *myNumber = [_testData objectAtIndex:indexPath.row];
if(myNumber) {
    cell.textLabel.text= [NSString stringWithFormat:@"%@", myNumber];
}

答案 1 :(得分:-1)

请检查两个值的比较

我认为此行发生了崩溃[__NSCFNumber isEqualToString:] 因为您使用此比较数字。此方法用于比较两个字符串。