Re: NSTableView delete multiple selected rows
Re: NSTableView delete multiple selected rows
- Subject: Re: NSTableView delete multiple selected rows
- From: Mike Shields <email@hidden>
- Date: Sat, 15 Dec 2001 03:37:38 -0700
How about this? This is tested in that I know it works for a limited set
of test cases. YMMV.
It easily handles undo. Implement this in your table delegate and hook
up removeSelectedRows: to some sort of menu or button.
NSString* const HPRowsKey = @"HPRowsKey";
NSString* const HPActionNameKey = @"HPActionNameKey";
- (IBAction) removeSelectedRows: (id) sender
{
NSMutableArray* newRows = [NSMutableArray arrayWithCapacity:[[self
rows] count]];
NSEnumerator* iter = [table selectedRowEnumerator];
NSNumber* rowNumber;
NSString* actionString;
int count = 0;
// These items will end up being the new contents of the table view
[newRows addObjectsFromArray:[self rows]];
// find the row in the current list of rows and remove it from the
new list of rows
while (rowNumber = [iter nextObject])
{
[newRows removeObject:[[self rows] objectAtIndex:[rowNumber
intValue]]];
count += 1;
}
// set up the undo string
if (count > 1)
{
actionString = NSLocalizedString(@"Remove Rows", @"Undo action
name for deleting multiple rows");
}
else
{
actionString = NSLocalizedString(@"Remove Row", @"Undo action
name for deleting a single row");
}
// and get the undo working correctly.
[self updateRowsAndRegisterForUndo:[NSDictionary
dictionaryWithObjectsAndKeys: newRows, HPRowsKey, actionString,
HPActionNameKey, nil]];
}
- (void) updateRowsAndRegisterForUndo:(NSDictionary*) undoDict
{
NSMutableArray* oldRowList = [self rows];
NSMutableArray* newRowList = [undoDict valueForKey: HPRowsKey];
NSString* actionName = [undoDict valueForKey: HPActionNameKey];
NSUndoManager* undoManager = [[table window] undoManager];
// Take the oldRow (actually the current items in the list) list
and register it with the
// undo manager for later use.
// Set this method as the selector to call when undo/redo is selected.
// Since we pass in a dictionary of the items that we want to be
the new row list,
// no matter if this is an undo or redo it works!
[undoManager registerUndoWithTarget: self
selector: @selector(updateRowsAndRegisterForUndo:)
object: [NSDictionary dictionaryWithObjectsAndKeys:
oldRowList, HPRowsKey, actionName, HPActionNameKey, nil]];
[undoManager setActionName: actionName];
// Now update the rows we'll be displaying and tell the table to
relaod.
[self setRows:newRowList];
[table reloadData];
}
Mike