【发布时间】:2013-12-19 16:31:49
【问题描述】:
我正在我的 iOS 应用程序中测试 UILongPressGestureRecognizer 的使用。这是场景: 1.- 一个 tableview 控制器显示来自核心数据实体的记录。 2.- 从核心数据实体中删除行的一种方法(通常使用commitEditingStyle),用于从核心数据实体中删除选定的行。 3.- 一种 UILongPressGestureRecognizer 方法,用于长按行、触发警报视图以及从核心数据实体中删除选定行。
这是相关的代码: 第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 按钮后,会显示警报视图,执行 performfetch 并重新加载表,但不显示最后删除的对象。之后,再次执行应用程序,证明删除的行确实被删除了,不再显示。
执行点 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
}
}
}
我无法找到意外行为的原因。欢迎任何帮助。谢谢
【问题讨论】:
标签: uitableview core-data ios7 uigesturerecognizer