2015-01-15 38 views
0

在xcode中,我試圖添加一些方法來記錄從tableview中刪除的內容。訪問indexPath.row字段變量

這裏是我的刪除代碼

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

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

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

    NSManagedObject *device = [self.contacts objectAtIndex:indexPath.row]; 
    NSLog(@"Deleting %@ ", [device valueForKey:@"name1"]); 

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

}

除了這兩條線

 NSManagedObject *device = [self.contacts objectAtIndex:indexPath.row]; 
    NSLog(@"Deleting %@ ", [device valueForKey:@"name1"]); 

我試圖訪問在該領域的數據都相當標準的東西它被刪除之前的行,但NSLog返回null。我嘗試了其他變體,並且有一個版本返回了上一行中的變量!

我在這裏錯過了什麼?

回答

0

在我看來,您試圖訪問數據已刪除後。這

[context deleteObject:[self.contacts objectAtIndex:indexPath.row]]; 
+0

感謝道格和保羅之前將這個

NSManagedObject *device = [self.contacts objectAtIndex:indexPath.row]; NSLog(@"Deleting %@ ", [device valueForKey:@"name1"]); 

。由於某種原因,我認爲刪除發生在幾行較低的位置,但我想只是將其從tableview iteslf中刪除 –

2

您嘗試在刪除對象並通過保存提交此操作後打印對象。

你可以只移動下面幾行:

NSManagedObject *device = [self.contacts objectAtIndex:indexPath.row]; 
NSLog(@"Deleting %@ ", [device valueForKey:@"name1"]); 

您的通話以上[context save:&error]