2013-10-15 27 views
1

在將節標題添加到我的應用程序中的某個表之前,我可以使用commitEditingStyle函數刪除行。我決定實現節標題,以方便用戶查看按日期添加到表中的項目。我測試了這個功能,它工作正常。但現在試圖刪除一行導致系統崩潰:使用節標題刪除表格視圖中的行

斷言失敗 - [UITableView的_endCellAnimationsWithContext:]> /SourceCache/UIKit_Sim/UIKit-2903.2/UITableView.m:1330

我對於obj-c開發來說是新的,並且不願意添加這些部分,因爲我知道這會增加代碼的複雜性,並且花了我一些時間才弄明白。但我想這就是你學習的方式。

下面是的cellForRowAtIndexPath函數的代碼:

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

    if (cell == nil) { 
     cell = [[AgendaCell alloc] 
       initWithStyle:UITableViewCellStyleDefault 
       reuseIdentifier:CellIdentifier]; 
    } 
    NSString *strDate = [dateArray objectAtIndex:indexPath.section]; 
    NSMutableArray *dateSection = [tempDict objectForKey:strDate]; 

    NSManagedObject *object = [dateSection objectAtIndex:indexPath.row]; 
    cell.sessionNameLabel.text = [object valueForKey:@"sessionname"]; 
    cell.sessionNameLabel.textColor = [UIColor blueColor]; 
    cell.sessionDateLabel.text = [object valueForKey:@"sessiondate"]; 
    cell.sessionDateLabel.textColor = [UIColor brownColor]; 
    cell.sessionTimeLabel.text = [object valueForKey:@"sessiontime"]; 
    cell.sessionTimeLabel.textColor = [UIColor brownColor]; 

    return cell; 

} 

這裏是我的表刷新功能代碼:

- (void) refreshTable 
    { 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    // Edit the entity name as appropriate. 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Sessnotes" inManagedObjectContext:self.managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"agenda == 'Yes'"]]; 

    // Edit the sort key as appropriate. 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sessiondate" ascending:YES]; 
    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil]; 

    [fetchRequest setSortDescriptors:sortDescriptors]; 

    NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil]; 

     [self.refreshControl endRefreshing]; 
     self.objects = results; 

    if (results.count == 0) { 
     NSString *message = @"You have not added any sessions to your planner."; 
     UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Notification" 
                 message:message 
                 delegate:self 
              cancelButtonTitle:@"Ok" 
              otherButtonTitles:nil,nil]; 
     [alertView show]; 
    } 
    else if (results.count > 0){ 
    tempDict = nil; 
    tempDict = [[NSMutableDictionary alloc] init]; 

    NSString *strPrevDate= [[results objectAtIndex:0] valueForKey:@"sessiondate"]; 
    NSLog(@"strPrevDate value is: %@", strPrevDate); 
    NSString *strCurrDate = nil; 
    NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 
    //Add the Similar Date data in An Array then add this array to Dictionary 
    //With date name as a Key. It helps to easily create section in table. 
    for(int i=0; i< [results count]; i++) 
    { 
     strCurrDate = [[results objectAtIndex:i] valueForKey:@"sessiondate"]; 

     if ([strCurrDate isEqualToString:strPrevDate]) 
     { 
      [tempArray addObject:[results objectAtIndex:i]]; 
     } 
     else 
     { 
      [tempDict setValue:[tempArray copy] forKey:strPrevDate]; 

      strPrevDate = strCurrDate; 
      [tempArray removeAllObjects]; 
      [tempArray addObject:[results objectAtIndex:i]]; 
     } 
    } 
    //Set the last date array in dictionary 
    [tempDict setValue:[tempArray copy] forKey:strPrevDate]; 

    NSArray *tArray = [tempDict allKeys]; 
    //Sort the array in ascending order 
    dateArray = [tArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 
    } 

     [self.tableView reloadData]; 

} 

下面是commitEditingStyle函數的代碼:

- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     //add code here for when you hit delete 

     NSManagedObject *object = [self.objects objectAtIndex:indexPath.row]; 

     NSManagedObjectContext *context = [self managedObjectContext]; 

     [context deleteObject:[context objectWithID:[object objectID]]]; 

     NSError *error = nil; 
     // Save the object to persistent store 
     if (![context save:&error]) { 
      NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]); 
     } 

     NSMutableArray *array = [self.objects mutableCopy]; 
     [array removeObjectAtIndex:indexPath.row]; 
     self.objects = array; 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 
+1

你能提供一些更多的崩潰信息? – paulrehkugler

+0

如果你使用核心數據,那麼你應該真的使用NSFetchedResultsController。看看這個... http://www.raywenderlich.com/999/core-data-tutorial-for-ios-how-to-use-nsfetchedresultscontroller – Fogmeister

回答

-1
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
//replace with it: 
[tableView reloadData]; 
+1

或者稍微少一些的核選項(更多本地化動畫)' [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic]' –

+0

謝謝。因此,上述工作時刪除具有不同標題部分的行(儘管標題部分保留,直到我刷新表,然後他們不顯示)。但是,如果行在同一部分中,並且我嘗試從該部分的首行開始按順序刪除多個行,則首行會刪除正常,但嘗試刪除第二行會導致應用程序崩潰。如果我刪除第一行以外的所有行,然後刪除最後一行,它可以正常工作。任何幫助? – bachma0507

+0

解決了崩潰和混亂,但我不得不放棄漂亮的插入/刪除動畫,所以仍然在尋找更好的解決方案。 –