advertisement

Article:
  Working with Tables: Writing an Address Book Application
Subject:   Ok fixed it
Date:   2005-06-19 17:49:39
From:   jz87
Response to: Works but with a little bug

I modified the code a bit, this will work if you have Panther or Tiger.


- (IBAction)deleteRecord:(id)sender
{
int status;
NSEnumerator* enumerator;
NSNumber* index;
NSMutableIndexSet* indexSet = [NSMutableIndexSet indexSet];

if ([tableView numberOfSelectedRows] == 0)
return;
NSBeep();
status = NSRunAlertPanel(@"Warning!", @"Are you sure you want to delete the selected record(s)?", @"OK", @"Cancel", nil);
if (status == NSAlertDefaultReturn)
{
enumerator = [tableView selectedRowEnumerator];
while (index = [enumerator nextObject])
{
[indexSet addIndex:[index intValue]];
}
[records removeObjectsAtIndexes:indexSet];
[tableView reloadData];
}
}


The documentation says that NSMutableIndexSet is only available since Panther. The remove object from array probably removes all matching objects, so it would remove all duplicates of an entry even if you just selected one. This version only removes the entries that were actually selected.