Re: NSTableView delete multiple selected rows
Re: NSTableView delete multiple selected rows
- Subject: Re: NSTableView delete multiple selected rows
- From: Charles Srstka <email@hidden>
- Date: Sat, 15 Dec 2001 03:09:31 -0600
On Saturday, December 15, 2001, at 02:31 AM, Max Horn wrote:
At 23:08 Uhr -0800 14.12.2001, Sam Goldman wrote:
There might be a simpler way (could try to get an NSRange for selected,
dunno if it exists).
It seems to me that this would be pretty simple to imagine, at least
if you
have done other programming. Maybe the fact that it's object-oriented
is
tripping you up. If so, just think about deleting items in one array
from
another.
--------------
int numRows = [tableView numberOfRows] - 1; // I don't remember if I
need
to take away 1 or not.
int i = 0;
while(int i < numRows)
{
if ([tableView isRowSelected: i])
[myDataSourceArray removeObjectAtIndex: i];
int i++;
}
[tableView reloadData];
This code is *NO* good!
Besides the obvious mistakes (int i++; instead of i++; or "while (int
i...)) this will not work at all, because when you start removing stuff
from the array, you change the indices of it! Hence you first remove
the wrong objects, and finally you will get an index out of bounds
exception if you remove e.g. the last item as one of several.
So either do this (it's not test either, so it might be no good, too
<g>):
int i = [tableView numberOfRows];
while(i-- > 0)
{
if ([tableView isRowSelected:i])
[myDataSourceArray removeObjectAtIndex:i];
}
[tableView reloadData];
Or you can use - (void)removeObjectsFromIndices:(unsigned *)indices
numIndices:(unsigned)count. You will have to use an array of unsigned
ints, representing the indices you want to remove. As to "how do you
get it" - well, you have to create it yourself :)
Max
-- -----------------------------------------------
Max Horn
Software Developer
email: <mailto:email@hidden>
phone: (+49) 6151-494890
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
I think that this might be faster. I wish that there were a more elegant
way to get the reverse enumerator, though.
NSArray *itemArray = [[tableView selectedRowEnumerator] allObjects];
NSEnumerator *objEnum = [itemArray reverseObjectEnumerator];
NSNumber *num;
while(num=[objEnum nextObject])
{
[myDataSourceArray removeObjectAtIndex:[num intValue]];
}
Like the previous examples, it's not tested...