NSArrayController select inserted indices after insertion not reflected in NSTableView

315 views Asked by At

I have two NSTableViews both bound to their own NSArrayControllers and the user can drag & drop items from table A to table B. Now I want the dropped items in table B to be selected after insertion (so that the table can scroll to them). But it's not working.

  • The table view selection indexes are bound to the array controller indexes in IB.
  • Select Inserted Objects is checked on the array controllers in IB.
  • If I check arrayControllerB.selectedObjects it lists only the first inserted object, even if more were inserted.
  • Selection is never reflected in the associated table view.

Now, I could try to temporarily store the newly inserted objects in an array and then select them in the arrayControllerB via setSelectedObjects(), like this ...

func insertIndicesToTargetAC(indexSet:NSIndexSet)
{
    let a = indexSet.toArray();
    var tmpObjects = [AnyObject]();
    for i in a
    {
        let sourceItem = sourceAC.arrangedObjects.objectAtIndex(i);
        let obj:NSManagedObject = createNewFromSourceItem(sourceItem);
        targetAC.addObject(obj);
        tmpObjects.append(obj);
    }
    sourceAC.removeObjectsAtArrangedObjectIndexes(indexSet);
    targetAC.setSelectedObjects(tmpObjects);
    println("\(targetAC.selectedObjects)");
}

}

Here, setSelectedObjects() does show me the correct amount of newly inserted objects in the target AC but it still doesn't reflect the selection in the associated table view. Can somebody inaugurate me into the holy sanctuary of trivial Cocoa operations that persistently refuse to work?

1

There are 1 answers

0
BadmintonCat On

I figured it out. I shall not attempt to tableView.reloadData() after the insertion. That will erase the selection.