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];
}
1 Like
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!
1 Like
[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.