2016-08-10 65 views
1

在這裏,當我點擊刪除按鈕時,應用程序崩潰給出錯誤:嘗試從對象[0]插入零對象' 這是我的代碼。從表視圖中刪除數據崩潰應用程序

@implementation ViewDetailViewController 

- (NSManagedObjectContext *)managedObjectContext 
{ 
    NSManagedObjectContext *context = nil; 
    id delegate = [[UIApplication sharedApplication] delegate]; 
    if ([delegate performSelector:@selector(managedObjectContext)]) { 
     context = [delegate managedObjectContext]; 
    } 
    return context; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    // Fetch the devices from persistent data store 
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Personal"]; 
    self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; 

    self.searchResult = [NSMutableArray arrayWithArray:self.devices]; 

    [self.tableView reloadData]; 


    // Do any additional setup after loading the view, typically from a nib. 
} 


- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    // Fetch the devices from persistent data store 
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Personal"]; 
    self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; 

    self.searchResult = [NSMutableArray arrayWithArray:self.devices]; 

    [self.tableView reloadData]; 


    // Do any additional setup after loading the view. 
} 

這是我的表視圖委託方法。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.searchResult count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    // Configure the cell... 
    NSManagedObject *device = [self.searchResult objectAtIndex:indexPath.row]; 

    { 
     [cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"name"]]]; 
     [cell.detailTextLabel setText:[device valueForKey:@"attribute"]]; 
    } 
    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; 
     } 
     static NSString *CellIdentifier = @"Cell"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
     NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell]; 

     //Remove device from table view 
     [self.devices removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 
+0

錯誤在代碼中出現在哪裏? – Carter

+0

當我點擊刪除按鈕的應用程序崩潰...當我重建應用程序行被刪除。意味着它被刪除,但當我點擊刪除@Carter時崩潰了應用程序 – virat

+0

當應用程序崩潰時,你知道哪行代碼會導致它崩潰嗎? – Carter

回答

1

在您的數據源方法,您使用self.searchResult

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.searchResult count]; 
} 

commitEditingStyle:[self.devices removeObjectAtIndex:indexPath.row];

刪除數據,您需要更改這個

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     return [self.devices count]; 
    } 

或更改

[self.searchResult removeObjectAtIndex:indexPath.row]; 

還有一件事你沒有必要得到cellIndexPath這PARAMS傳遞給方法尚未

-(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:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

    } 
} 
+0

我已經嘗試了兩種方式建議你,但它不工作仍然在點擊刪除時崩潰。 – virat

+0

我有點改變了答案 – iSashok

+0

沒有幫助...看到設備我看到設備是我的主陣列和searchResult是我用於搜索條數據的數組 – virat

0

請將此代碼可能會使用全

[self.searchResult removeObjectAtIndex:indexPath.row]; 
[tableview reload]; 
+0

沒有幫助...查看設備我看到設備是我的主陣列,searchResult是我用於searchBar數據的數組 – virat

0

嘗試刪除這一行:[tableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 看來你不應該刪除commitEditingStyle中的單元格。這個委託會自動完成它。

+0

除非您使用'NSFetchedResultsController'及其委託方法,否則這是錯誤的。 – vadian

相關問題