[__NSCFString objectForKey:]:崩溃的文本字段搜索

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

当我在我的表视图中进行搜索的数据我收到此崩溃。我使用的文本框为搜索栏。这里是我的代码:

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];
        }
    }
ios arrays objective-c nsdictionary uisearchbar
1个回答
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];
    }
}

尝试和分享您的结果。

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