我在我的iOS應用程序中測試UILongPressGestureRecognizer的使用。這是場景: 1.-一個顯示核心數據實體記錄的tableview控制器。 2.-一種從核心數據實體中刪除行的方法(正常使用commitEditingStyle),用於從核心數據實體中刪除選定的行。 3.一個UILongPressGestureRecognizer方法用於長按該行,激發警報視圖,並從核心數據實體中刪除選定的行。在iOS7中使用核心數據刪除表視圖中的行的奇怪行爲
這是關於代碼: 點2:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
NSError *error = nil;
if (![context save:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
NSError *error1 = nil;
if (![[self fetchedResultsController] performFetch:&error1])
{
NSLog(@"Unresolved error %@, %@", error1, [error userInfo]);
abort();
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"To-Do Deleted" message: @"You have marked To-Do as deleted...!" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
[self.tableView reloadData];
}
}
此方法按預期工作。觸摸紅色的DELETE按鈕後,會顯示警報視圖,執行執行並重新載入表格,而不顯示上次刪除的對象。之後,再次執行該應用程序,證明刪除的行真的被刪除,不再顯示。
執行點3.-,顯示與點2.-相同的行爲,除了在第二次執行應用程序之後,刪除的行再次出現,這意味着它不會從核心數據中刪除。這是點3的代碼.-
- (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:@"Cell"] autorelease];
}
//long press
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 1.00;
//seconds
[cell addGestureRecognizer:lpgr];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];
if (indexPath == nil)
NSLog(@"long press on table view but not on a row");
else
{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
NSLog(@"UIGestureRecognizerStateEnded");
//Do Whatever You want on End of Gesture
}
else if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Announcement" message: @"You have long-pressed the row...!" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
NSLog(@"UIGestureRecognizerStateBegan.");
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
NSLog(@"long press on table view at row %d", indexPath.row);
// Update ToDoStatus
NSError *error1 = nil;
if (![[self fetchedResultsController] performFetch:&error1])
{
NSLog(@"Unresolved error %@, %@", error1, [error1 userInfo]);
abort();
}
[self.tableView reloadData];
//Do Whatever You want on Began of Gesture
}
}
}
我無法找到意外行爲的原因。歡迎任何幫助。謝謝
也就是說,你是對的。非常感謝,我一整天都在處理這個問題.... – mvasco