gpt4 book ai didi

ios - [__NSCFString objectForKey :]: crash on textfield search

转载 作者:行者123 更新时间:2023-11-28 23:37:12 27 4
gpt4 key购买 nike

当我在我的 TableView 中搜索数据时遇到此崩溃。我正在使用文本字段作为搜索栏。这是我的代码:

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;
}

我需要搜索一个数字和一个字符串。但它因以下错误而崩溃:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 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];
}
}

最佳答案

您可以用此替换您的 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;
}

问题是您直接在返回 NSString 值的 cellForRowAtIndexPath 方法的过滤器部分中获取对象,然后再次尝试获取 objectForKey从这个 NSString这会导致崩溃,因为 NSString 没有已知的方法 objectForKey

编辑:

这与崩溃无关,但您还应该根据过滤器更新此方法。

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(isFilter) {
return [searchArray count];
} else {
return [self.EtNotifRepTableDataArray count];
}
}

尝试并分享您的结果。

关于ios - [__NSCFString objectForKey :]: crash on textfield search,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54625465/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com