2013-08-25 62 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