删除最后一行后发出问题并崩溃

时间:2016-10-24 11:10:18

标签: ios objective-c xcode uitableview

我在过去的2个小时里一直在努力。为什么最后一行没有从我的表视图中删除。一行只保留在表视图中,不会删除。虽然我从数组中删除了对象,但没有任何内容在数组中。

代码中有什么问题?

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        Geofencing *getObject = [routesArray objectAtIndex:indexPath.row];
        [getObject deleteGeofencingFormDatabase:getObject.ah_routeid];

        [routesArray removeObjectAtIndex:indexPath.row];

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1*NSEC_PER_SEC), dispatch_get_main_queue(), ^{


            [routeTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
            [self displayEmptyRouteAlert];

        });
    }
}

更新1:

 -(void)displayEmptyRouteAlert
 {
  routesArray = [[Geofencing sharedInstance] fetchGeofencingFromDatabase];

if(routesArray.count > 0){
    [routeTableView reloadData];
}
else{

    NSString *messageStr = @"Let's start by creating new route.";
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"No route found"
                                                                   message:messageStr
                                                            preferredStyle:UIAlertControllerStyleAlert];


    UIAlertAction *buttonAdd = [UIAlertAction actionWithTitle:@"Add New Route" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        [self openToAddNewRoute];
    }];
    [alert addAction:buttonAdd];



    UIAlertAction *buttoncancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
    }];
    [alert addAction:buttoncancel];

    [self presentViewController:alert animated:YES completion:nil];
}}

1 个答案:

答案 0 :(得分:0)

我找出适合我的解决方案。问题出在其他地方,并在某种程度上修改了commitEditingStyle:

routeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 80, self.view.bounds.size.width, self.view.bounds.size.height-160)];
routeTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
routeTableView.dataSource = self;
routeTableView.delegate = self;
routeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
routeTableView.allowsMultipleSelectionDuringEditing = NO;
[self.view addSubview:routeTableView];

首先,我在viewDidLoad - 方法中移动上述代码,之前它是用viewWillAppear编写的。

比我改变了
  - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

   if (editingStyle == UITableViewCellEditingStyleDelete) 
    {
    Geofencing *getObject = [routesArray objectAtIndex:indexPath.row];
    [routesArray removeObjectAtIndex:indexPath.row];
    [deleteArray addObject:getObject];  //deleteArray collecting the object which will removed in viewDidDisappear
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    dispatch_async(dispatch_get_main_queue(), ^{
        [routeTableView reloadData];
    });
  }
 }

最后

  - (void)viewDidDisappear:(BOOL)animated
   {
     dispatch_async(dispatch_get_main_queue(), ^{
    for (Geofencing *getObject in deleteArray) {
        [getObject deleteGeofencingFormDatabase:getObject.ah_routeid];
        [routeTableView reloadData];
    }
    [deleteArray removeAllObjects];
   });
}