尝试调用 tableView.reloadData 时出现 NSInvalidArgumentException?
Posted
技术标签:
【中文标题】尝试调用 tableView.reloadData 时出现 NSInvalidArgumentException?【英文标题】:NSInvalidArgumentException when tried to call tableView.reloadData? 【发布时间】:2016-06-28 03:47:21 【问题描述】:我看到了所有重复的问题并尝试了所有解决方案。但我无法成功运行我的应用程序。
单击按钮时,我从数据库类获取 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:@"*** 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
buttonpressed 是我按下按钮时调用的函数。
我试过了,
[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
感谢您的时间(:
【问题讨论】:
您的数组包含NSNumber
s,而不是NSString
s。
是的...它包含数字...
检查UILabel
的text
属性的文档。
@MuhammadAdnan 两者都是同一个人..
@Avi 对,我认为数组中的数据是数字并分配给文本。所以 Ganesh 将您的 NSNumber 转换为 NSString
【参考方案1】:
cell.textLabel.text=[_testData objectAtIndex:indexPath.row];
而不是上面的行,尝试如下:
NSNumber *myNumber = [_testData objectAtIndex:indexPath.row];
if(myNumber)
cell.textLabel.text= [NSString stringWithFormat:@"%@", myNumber];
【讨论】:
【参考方案2】:请检查两个值的比较
我认为这行会发生崩溃[__NSCFNumber isEqualToString:]
因为您正在使用它来比较数字。该方法用于比较两个字符串。
【讨论】:
以上是关于尝试调用 tableView.reloadData 时出现 NSInvalidArgumentException?的主要内容,如果未能解决你的问题,请参考以下文章
tableView.reloadData() 不起作用,可能没有在主线程上调用
Swift:tableView.reloadData() 太慢了
未调用 tableView reloadData cellForRowAtIndexPath
调用[tableView reloadData];在 modalViewController 的 viewController 上