Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UISearchBar在实现文本时崩溃_Ios_Objective C_Uitableview_Nsmutablearray_Uisearchbar - Fatal编程技术网

Ios UISearchBar在实现文本时崩溃

Ios UISearchBar在实现文本时崩溃,ios,objective-c,uitableview,nsmutablearray,uisearchbar,Ios,Objective C,Uitableview,Nsmutablearray,Uisearchbar,我有一个带有tableview的应用程序,你可以添加和删除项目,但当我尝试实现搜索栏时,每当我键入一个字母时,它就会崩溃。以下是我正在使用的代码: - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { if (searchText.length == 0) { isFiltered = NO; } else { isFiltered = Y

我有一个带有tableview的应用程序,你可以添加和删除项目,但当我尝试实现搜索栏时,每当我键入一个字母时,它就会崩溃。以下是我正在使用的代码:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if (searchText.length == 0) {
        isFiltered = NO;
    } else {
        isFiltered = YES;

        filteredPatients = [[NSMutableArray alloc] init];

        for (Patient *patient in patients) {
            NSRange patientNameRange = [patient.patientName rangeOfString:searchText options:NSCaseInsensitiveSearch];

            if (patientNameRange.location != NSNotFound) {
                [filteredPatients addObject:patient];
            }
        }
    }
    [self.tableView reloadData];
}
这很好,但当您键入一封包含患者的信时,它会在此行中断:

cell.textLabel.text = [filteredPatients objectAtIndex:indexPath.row];
以下是上下文中的代码:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"cell"];
    if ( nil == cell ) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    NSLog(@"indexPath.row = %d, patients.count = %d", indexPath.row, patients.count);
    Patient *thisPatient = [patients objectAtIndex:indexPath.row];

    if (isFiltered == YES) {
        cell.textLabel.text = [filteredPatients objectAtIndex:indexPath.row];
    } else {
        cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", thisPatient.patientName, thisPatient.patientSurname];
    }

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.textColor = [UIColor blackColor];
    if (self.editing) {
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    return cell;
}
并返回此错误

-[Patient isEqualToString:]: unrecognized selector sent to instance 0x756c180
如果你想要更多的代码,那就去问吧


提前感谢

您正在迭代收集
患者
,其中似乎包含
患者
实例,而不是
NSString
实例。所以我会做一些类似的事情:

-(无效)搜索栏:(UISearchBar*)搜索栏文本更改:(NSString*)搜索文本{
如果(searchText.length==0){
isFiltered=否;
}否则{
isFiltered=是;
filteredPatients=[[NSMutableArray alloc]init];
用于(患者*患者中的患者){
NSRange patientNameRange=[patient.name rangeOfString:searchText选项:NSCaseInsensitiveSearch];
if(patientNameRange.location!=NSNotFound){
[筛选患者添加对象:患者];
}
}
}
[self.tableView重载数据];
}

Aha如果我输入一封属于其中一项的信,它将停止在另一行,我将在问题中发布该行等待它工作,我需要将addObject更改为patient.name;