我在表格视图中搜索数据时遇到此崩溃。我正在使用文本字段作为搜索栏。这是我的代码:
NSMutableArray *searchArray;
NSString *searchTextString;
BOOL isFilter;
@property NSMutableArray *TableDataArray;
- (void)viewDidLoad {
[super viewDidLoad];
[self.searchTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[self updateSearchArray];
[self.tableView reloadData];
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [searchArray count];
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"SubNotificationCell";
SubNotificationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *dict;
if(isFilter) {
dict = [[self->searchArray objectAtIndex:indexPath.row] objectForKey:@"Eqktx"];
} else {
dict = [self.EtNotifRepTableDataArray objectAtIndex:indexPath.row];
}
NSString* notifValue = [dict objectForKey:@"Qmnum"];
NSString* equipNameValue = [dict objectForKey:@"Eqktx"];
NSString *values = @":";
cell.notifValueLabel.text = [NSString stringWithFormat: @"%@ %@", values, notifValue];
cell.equipNameValueLabel.text = [NSString stringWithFormat: @"%@ %@", values, equipNameValue];
return cell;
}
-(void)textFieldDidChange:(UITextField*)textField {
searchTextString = textField.text;
[self updateSearchArray];
}
-(void)updateSearchArray {
if (searchTextString.length != 0) {
isFilter=YES;
searchArray = [NSMutableArray array];
for ( NSDictionary* item in _EtNotifRepTableDataArray ) {
if ([[[item objectForKey:@"Eqktx"] lowercaseString] rangeOfString:[searchTextString lowercaseString]].location != NSNotFound) {
[searchArray addObject:item];
}
}
} else {
isFilter=NO;
searchArray = _EtNotifRepTableDataArray;
}
[self.tableView reloadData];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
我需要搜索数字和字符串。但是由于以下错误而崩溃:
***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__ NSCFString objectForKey:]: 无法识别的选择器发送到实例0x280e083f0'
在我的viewdidload
@property NSMutableArray *EtNotifRepTableDataArray;
_EtNotifRepTableDataArray = [[NSMutableArray alloc]init];
for (NSDictionary *dict in arr) {
[dict description];
NSString *Qmart = [dict objectForKey:@"Qmart"];
NSString *Phase = [dict objectForKey:@"Phase"];
if ([Qmart isEqualToString:_qmartValue] && [Phase isEqualToString:_phaseValue]){
[self.EtNotifRepTableDataArray addObject:dict];
}
}
答案 0 :(得分:1)
您将cellForRowAtIndexPath
方法替换为此。
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"SubNotificationCell";
SubNotificationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *dict;
if(isFilter) {
// HERE was the problem.
//dict = [[self->searchArray objectAtIndex:indexPath.row] objectForKey:@"Eqktx"];
dict = [self.searchArray objectAtIndex:indexPath.row]
} else {
dict = [self.EtNotifRepTableDataArray objectAtIndex:indexPath.row];
}
NSString* notifValue = [dict objectForKey:@"Qmnum"];
NSString* equipNameValue = [dict objectForKey:@"Eqktx"];
NSString *values = @":";
cell.notifValueLabel.text = [NSString stringWithFormat: @"%@ %@", values, notifValue];
cell.equipNameValueLabel.text = [NSString stringWithFormat: @"%@ %@", values, equipNameValue];
return cell;
}
问题是您直接在cellForRowAtIndexPath
方法的过滤器部分中获取对象,该方法返回一个NSString值,然后再次尝试从此NSString中获取objectForKey
这会导致崩溃,因为NSString没有已知的方法objectForKey
编辑:
它与崩溃无关,但您还应该基于过滤器更新此方法。
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(isFilter) {
return [searchArray count];
} else {
return [self.EtNotifRepTableDataArray count];
}
}
尝试并分享您的结果。