2nd Edition chapter 32 challenge help (print)


#1

I need to remove the current selected item in tableview. I have linked the button to a method I’ve created (removeTask) but I don’t know how to make it delete the current selection. If i use removeObjectAtIndex:0 it’ll only delete the very top which kind of works but I would like to know how to delete the current selected item. Thanks!

  • (void)addTask:(id)sender
    {
    //NSLog(@“Add Task button clicked!”);
    // If there is no array yet, create one
    if (!self.tasks) {
    self.tasks = [NSMutableArray array];
    }

    [self.tasks addObject:@“New Item”];

    // -reloadData tells the table view to refresh and ask its dataSource (which hapens to be this BNRDocument object in this case) for new data to display
    [self.taskTable reloadData];

    // -updateChangeCount: tells the application wheather or not the document has unsaved changes, NSChangeDone flags the document as unsaved
    [self updateChangeCount:NSChangeDone];
    }

  • (void)removeTask:(id)sender
    {
    //NSLog(@“Delete task button clicked”);

    [self.tasks removeObjectAtIndex:<#(NSUInteger)#>];

    [self.taskTable reloadData];

    [self updateChangeCount:NSChangeDone];
    }


#2

I’ve figured it out. All I had to do is:

[self.tasks removeObjectAtIndex:[_taskTable selectedRow]];


#3

Here is my solution…worked for me:

BNRDocument.h

[code]…

  • (IBAction)deleteTask:(id)sender;
    …[/code]

BNRDocument.m

[code]…

  • (void)deleteTask:(id)sender
    {
    if (self.taskTable.selectedRow >= 0) {
    [self.tasks removeObjectAtIndex:self.taskTable.selectedRow];
    [self.taskTable reloadData];
    [self updateChangeCount:NSChangeDone];
    }
    }
    …[/code]

#4

You could also use NSIndexSet in case the user selects more than one row
From BNRDocument.m

[code]- (void)deleteTask:(id)sender
{
//Find out which tasks are being selected for removal
NSIndexSet *selectedRows = [self.taskTable selectedRowIndexes];

//Remove objects from the tasks array corresponding to the indexes for the selected rows
[self.tasks removeObjectsAtIndexes:selectedRows];

//Refresh the table view and tell application about unsaved changes
[self.taskTable reloadData];
[self updateChangeCount:NSChangeDone];

}[/code]


#5

Thanks Mackzyme, that’s a good idea! After seeing your solution I changed what I did.


#6

I guess there’s more in updateChangeCount:NSChangeDone, if you delete it from the deleteTask: method, when you re-run the application the changes does not take effect unless you finish with that. So, can anyone explain me? It will be very helpful, thanks!!!


#7

My solution and first post :smiley: :stuck_out_tongue:

[code]- (void)deleteTask:(id)sender
{
// create a variable for the user’s selected row
NSInteger selectedRow = [self.taskTable selectedRow];

if (!self.tasks) {
    self.tasks = [NSMutableArray array];
}

// Avoid errors by ensuring count and selected row have
// values before calling these methods.
if([self.tasks count] != 0 && selectedRow != -1) {
   
    
    [self.tasks removeObjectAtIndex:selectedRow];
    
    [self.taskTable reloadData];
    
    [self updateChangeCount:NSChangeDone];
    
}

}

   [/code]