2014-02-20 2 views
0

Всякий раз, когда я пытаюсь ввести второй символ в SearchBar, приложение вылетает с ошибкой «завершение с неперехваченным исключением». SearchBar ссылается на данные таблицы Cell, если это вообще помогает. Как это исправить?SearchBar сбой после первого символа -Сохранить данные

@interface DeviceViewController() 
@property (strong) NSMutableArray *devices; 
@end 

@implementation DeviceViewController 
{ 
    NSArray *searchResults; 
} 
- (NSManagedObjectContext *)managedObjectContext { 
    NSManagedObjectContext *context = nil; 
    id delegate = [[UIApplication sharedApplication] delegate]; 
    if ([delegate performSelector:@selector(managedObjectContext)]) { 
     context = [delegate managedObjectContext]; 
    } 
    return context; 
} 
- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero]; 
    //label.backgroundColor = [UIColor clearColor]; 
    label.font = [UIFont fontWithName:@"HelveticaNeue-thin" size:28]; 
    //label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; 
    label.textColor = [UIColor blackColor]; 
    self.navigationItem.titleView = label; 
    label.text = @"TapNotes"; 
    [label sizeToFit]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    // Fetch the devices from persistent data store 
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"]; 
    self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; 

    [self.tableView reloadData]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     return [searchResults count]; 

    } else { 
     return self.devices.count; 
    } 
    //return self.devices.count; 
    } 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell==nil) { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } 
    NSManagedObject *device = [self.devices objectAtIndex:indexPath.row]; 
    [cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"name"]]]; 
    [cell.detailTextLabel setText:[NSString stringWithFormat:@"%@",[device valueForKey:@"version"]]]; 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     device = [searchResults objectAtIndex:indexPath.row]; 
    } else { 
     [self.devices objectAtIndex:indexPath.row]; 
    } 

    // cell.thumbnailImageView.image = [UIImage imageNamed:recipe.image]; 

    return cell; 
} 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSManagedObjectContext *context = [self managedObjectContext]; 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete object from database 
     [context deleteObject:[self.devices objectAtIndex:indexPath.row]]; 

     NSError *error = nil; 
     if (![context save:&error]) { 
      NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]); 
      return; 
     } 

     // Remove device from table view 
     [self.devices removeObjectAtIndex:indexPath.row]; 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 

/* 
// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 
*/ 

/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"UpdateDevice"]) { 
     NSManagedObject *selectedDevice = [self.devices objectAtIndex:[[self.tableView indexPathForSelectedRow] row]]; 
     NSIndexPath *indexPath = nil; 
     if (self.searchDisplayController.active) { 
      indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow]; 
      _devices = [searchResults objectAtIndex:indexPath.row]; 
     } else { 
      indexPath = [self.tableView indexPathForSelectedRow]; 
      _devices = [_devices objectAtIndex:indexPath.row]; 
     } 
     DeviceDetailViewController *destViewController = segue.destinationViewController; 
     destViewController.device = selectedDevice; 
    } 
} 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 55; 
} 
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText]; 
    searchResults = [_devices filteredArrayUsingPredicate:resultPredicate]; 
} 
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self filterContentForSearchText:searchString 
           scope:[[self.searchDisplayController.searchBar scopeButtonTitles] 
             objectAtIndex:[self.searchDisplayController.searchBar 
                selectedScopeButtonIndex]]]; 

    return YES; 
} 
@end 
+0

Что полный крах? можете ли вы скопировать и вставить? – CW0007007

+0

завершение с неперехваченным исключением типа NSException –

+0

и что такое исключение? и где это исключение? – Daniel

ответ

2

У вас много проблем с управлением таблицей.

Как я упоминал HERE, используйте NSFetchedResultsController (или откройте пустой проект, который включает CoreData, и посмотрите, как будет легко управлять таблицей, когда у вас есть).

Вы не можете фильтровать свой массив поиска, не уведомляя таблицу о необходимости ее перезагрузки.
Табличные должен знать кол-источника данных, как он будет ожидать, эти данные, чтобы существовать, когда он просит об этом в cellForRow...

Кроме того, вы лечить вашу _device переменные один раз как NSArray и один в качестве управляемого объекта (в cellForRow...).

в cellForRow... изменения _device к device ...
и тогда у вас будет новая проблема ...

(смотреть на советы, которые я дал вам в предыдущем вопросе)

+0

Изменение «_device» на «устройство», казалось, работало как шарм. И наконец, что мне нужно сделать для кода в UIStoryboardSegue, чтобы результаты поиска были доступны? Это последнее, что я обещаю. Это значит многое!! –

+0

Тот же ответ разрешит и другую проблему (только с 'selectedDevice') –

Смежные вопросы