AbdelElrafa
AbdelElrafa

Reputation: 891

Cell selected after opening another view and canceling

After I click on a cell to edit an object the cell opens the other VC to allow edit, but if the user clicks cancelled the cell is still selected and causes problems and crashes soon after.

Cell Editing:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if ([segue.identifier isEqualToString:@"addAssignment"])
    {
        AddEditViewController *addEditController = segue.destinationViewController;
        [addEditController setOtherdelegate:self];
    }


   if ([segue.identifier isEqualToString:@"edit"]) {
    UITableViewCell *cell = sender;
    AddEditViewController *addEditController = segue.destinationViewController;
    [addEditController setOtherdelegate:self];
    addEditController.edit = YES;
    AssignmentInfo *a = [self.alist objectAtIndex:[self.tableView indexPathForCell:cell].row];
    addEditController.assignmentEditing = a;

    [self.alist removeObject:a];
    [self.tableView reloadData];
    NSString *filePath = [self dataFilePath];
    [NSKeyedArchiver archiveRootObject:self.alist toFile:filePath];
    }
}

Cancel Button Pressed:

- (IBAction)addAssignmentCancelButtonPressed:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

Whole project if needed: http://abdelelrafa.com/AssignmentAppTwo.zip

Upvotes: 0

Views: 92

Answers (3)

Abdullah Shafique
Abdullah Shafique

Reputation: 6918

First what you have to do is remove this line:

[self.alist removeObject:a]; 

Next in your:-(void)newAssignment:(AssignmentInfo *)assignment remove:

  [self.tableView reloadData];

and add:

 [self.tableView reloadData];

to -(void)deletedAssignment:(AssignmentInfo *)assignment

Last in your:-(void)newAssignment:(AssignmentInfo *)assignment remove:

  NSString *filePath = [self dataFilePath];
[NSKeyedArchiver archiveRootObject:self.alist toFile:filePath];

and add:

NSString *filePath = [self dataFilePath];
[NSKeyedArchiver archiveRootObject:self.alist toFile:filePath];

to -(void)deletedAssignment:(AssignmentInfo *)assignment

Upvotes: 1

Holly
Holly

Reputation: 5300

Check the value of UITableViewController -clearsSelectionOnViewWillAppear.

That setting defaults to YES, so if it is wrong and you didn't manually set it somewhere, you probably have your View or View Controller life cycle messed up.

Upvotes: 0

TreeTree
TreeTree

Reputation: 557

You are calling the

[self.alist removeObject:a]; 

method every time you click on a cell. Therefore, you will get array out of bounds when you select the cell again as the object is removed.

I'm not too sure of how you want your application flow to be, but you could call:

[self.tableView reloadData];

just after you remove the 'a' object to refresh the table. That way, the table will show the most updated data.

Otherwise, I would suggest you to not call the removeObject, but actually pass the object to your AddEditViewController and modify it there.

Upvotes: 0

Related Questions