My Solution for Delete Selected Item


#1

Pretty much the same solution as the other ones that were already posted on here.

Document.m

- (IBAction)deleteTask:(id)sender
{
 
    NSInteger rowToRemove;
    rowToRemove = [self.taskTable selectedRow];
    [self.tasks removeObjectAtIndex:rowToRemove];
    
    [self.taskTable reloadData];
    
    [self updateChangeCount:NSChangeDone];
}

#2

What happens if someone doesn’t select a row and hit’s ‘Delete Task’? Your app crashes.

Easiest way to avoid that crash that I’ve found is add an if statement to check if a selection exists:

[code]// selectedRow method will return -1 if there is no selection!
// Only remove an object if a row is selected
if ([self.taskTable selectedRow] > -1) {

// Remove the selected row from the Tasks array
[self.tasks removeObjectAtIndex:[self.taskTable selectedRow]];

// Refresh the table and mark changes have been made
[self.taskTable reloadData];
[self updateChangeCount:NSChangeDone];

}[/code]

Just posting for posterity, and also to gain clarity in knowledge by trying to explain something about newly learned things to someone else!


#3

[quote=“jumbonugget”]What happens if someone doesn’t select a row and hit’s ‘Delete Task’? Your app crashes.

Easiest way to avoid that crash that I’ve found is add an if statement to check if a selection exists:

[code]// selectedRow method will return -1 if there is no selection!
// Only remove an object if a row is selected
if ([self.taskTable selectedRow] > -1) {

// Remove the selected row from the Tasks array
[self.tasks removeObjectAtIndex:[self.taskTable selectedRow]];

// Refresh the table and mark changes have been made
[self.taskTable reloadData];
[self updateChangeCount:NSChangeDone];

}[/code]

Just posting for posterity, and also to gain clarity in knowledge by trying to explain something about newly learned things to someone else![/quote]

Nicely done. Your comments are very good as well.