Is there a way to find the index of a row in a table view without having to loop through the entire list?
Let’s say I want to programmically select a row but I only know one of its keys and I don’t know which index that is on.
Here is how I am doing it now with a repeat loop:
property theIndexSet : class “NSIndexSet” of current application
– theList is an NSMutableArray linked to my Array Controller in IB
set myList to my theList as list
set myKey to chKey as string
– search and select channel
repeat with i from 1 to count of myList
if (chKey of item i of myList) = myKey as string then
set grabIndex to theIndexSet’s indexSetWithIndex_(i-1)
myTable’s selectRowIndexes_byExtendingSelection_(grabIndex, false)
set myList to {}
exit repeat
end
end
mainWindow's makeFirstResponder_(myTable)
I thought about doing a NSPredicate filter, but that seems to actually filter the list down to a smaller list. All I need is the index.
I’m not sure I understand the question. All the rows should contain the same keys (they represent the columns, in effect) – it’s the values that differ. Are you saying you want to select a row that has a particular value for a particular key?
correct.
I think I can do it with NSPredicate and indexOfObjectPassingTest.
You could probably also use valueForKey and then indexOfObjectEqualTo (from memory --something like that) on the result.
Shane,
Thank you. I will check that out; it looks like that might be simplier. Out of curiosity might explore both methods.
Cheers,
gt
The latter should probably just be indexOfObject:.