Add values to NSTableview in a way that lets me reorder them at will

84 Views Asked by At

I have an NSTableView and need to add values. I was thinking of doing it with a NSArrayController, but near my table there are four buttons: Move to top, Move up, Move down, Move to bottom. I must be able to reorder the entries in the table with these buttons.

The only thing I can think of is to use an array of dictionaries, where the first entry of the dictionary is the value displayed and the second entry of the dictionary is a double that is used to sort the array. If I move a value up or down, I take the sort value of the entry before and after the destination sum them, and then I divide by two.

I am not sure that the solution I was thinking of is appropriate. What would be the best approach to this scenario?

------EDIT-----

Working on it, and now I am having difficulties writing the updated "order" value into the arraycontroller. Beside that i am having trouble in actually sorting the table with the "order" column once that the value has been updated. Here is my code:

-(IBAction)singleMoveKeywordUp:(id)sender
{
NSInteger selectedRow = [singleKeywordTable selectedRow];
double firstnum;
double secondnum;
double newnum;

NSMutableDictionary *kwmutabledict = [[NSMutableDictionary alloc] init];

if (selectedRow < 2)
{
    firstnum = 0;
    kwmutabledict = [keywordscontroller.arrangedObjects objectAtIndex:0];
    secondnum = [[kwmutabledict valueForKey:@"order"] doubleValue];
}
  else
{

    kwmutabledict = [keywordscontroller.arrangedObjects objectAtIndex:selectedRow-2];
    firstnum = [[kwmutabledict valueForKey:@"order"] doubleValue];
    kwmutabledict = [keywordscontroller.arrangedObjects objectAtIndex:selectedRow-1];
    secondnum = [[kwmutabledict valueForKey:@"order"] doubleValue];

}


NSMutableDictionary *newkwmutabledict = [[NSMutableDictionary alloc] init];
newnum = (firstnum + secondnum)/2;



[newkwmutabledict setObject:[NSString stringWithFormat:@"%@",[kwmutabledict valueForKey:@"keyword"]] forKey: @"keyword"];
[newkwmutabledict setObject:[NSString stringWithFormat:@"%f",newnum] forKey: @"order"];

[keywordscontroller.arrangedObjects replaceObjectAtIndex:selectedRow withObject:newkwmutabledict] ; //<--------

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"order" ascending:YES];

[singleKeywordTable setSortDescriptors:[NSArray arrayWithObject:sort]];
}

This is failing at the row marked with an Arrow with the error message -[_NSControllerArrayProxy replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x100450520 I can't figure out whats wrong.

1

There are 1 best solutions below

0
On BEST ANSWER

keywordscontroller.arrangedObjects returns (NSArray *) which is NOT mutable, and NSArray does not contain selector "replaceObjectAtIndex:withObject:". Hence the exception/error "unrecognized selector".