2013-08-25 29 views
0

我有一个应用程序与tableview,你可以添加和删除项目,虽然当我试图实现一个搜索栏它崩溃时,只要我键入一个字母。下面是我使用的代码:UISearchBar实现文本时崩溃

- (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 

如果你想要更多的代码然后问。

在此先感谢

回答

1

你迭代上似乎包含Patient实例,而不是NSString实例集合patients。所以我会做这样的事情:

- (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.name rangeOfString:searchText options:NSCaseInsensitiveSearch]; 

      if (patientNameRange.location != NSNotFound) { 
       [filteredPatients addObject:patient]; 
      } 
     } 
    } 
    [self.tableView reloadData]; 
} 
+0

啊哈停止打破它有但如果我把信是项目则停在另一条线,我将张贴在我的问题 – Hive7

+0

的一个已经更新了我的问题 – Hive7

+0

任何想法可能是错的 – Hive7