How is NSTableView Content Sorted After Array Controller Add? - cocoa

After adding a managed object in array controller class, the bound table view 'places' it into the correct date sorted order.
However, as the table view is building it's rows, the new object has been placed at the bottom of the array controller's content array using:
Edit: Solution: Don't use the array controller's content. The issue was in:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
....
// originally
NSManagedObject *ci = [[self.arrayController content] objectAtIndex:row];
// should be: (note arrangedObjects replacing content)
NSManagedObject *ci = [self.arrayController.arrangedObjects objectAtIndex:row];
....
}
The image below shows an example of the placement. Console output from within -tableView: viewForTableColumn: row: at left (from original code above) and ordering in the table view on right. LHR is the new object (notice date order ascending).
Tried:
Saving the moc right after adding newObject
Calling Array Controller's arrangeObjects method
Attempting to manually setSortDescriptors
Binding the table view's selection index to the array controller (for grins)
This is also messing up the selectedRow even though the row with LHR is selected in the table view right after adding. The only way to correct the array controller's order is to manually sort the column.
How can the array controller's content be in sync with the table view? Even more, why might they not be in the same order?

The array controller "arranges" its content. It does this by calling its -arrangeObjects: method. The default implementation filters the contents using the array controller's filterPredicate and then sorts it using its sortDescriptors. A subclass could override that method to arrange the content differently.
The arranged contents can be accessed using the arrangedObjects property. This is what corresponds to rows in the table view. So, you should always use this if you're indexing by a table row. For example:
NSManagedObject *ci = [self.arrayController.arrangedObjects objectAtIndex:row];
The array controller's sortDescriptors may be set via bindings (e.g. if the table view's sortDescriptors binding is bound to the array controller). Depending on how you set up the table view bindings, this may be automatic. For NSCell-based table views, you typically bind the table columns value binding and don't bind the table views bindings. In that case, the table view automatically binds its content, selectionIndexes, and sortDescriptors bindings to the same controller as its columns. For view-based table views, you typically don't bind table columns and have to bind those table view bindings explicitly if you want them bound.
You can also set the array controller's sortDescriptors programmatically.

Try invoking rearrangeObjects on the NSArrayController and then doing the selection you need subsequent to that in a dispatch_async block. It's maddening, but some of the bindings are deferred to the next runloop turn, so you can't simply add an object, call rearrangeObjects, and select the new object in synchronous code. You have to use GCD to schedule things
See this question/answer for more discussion. It's is the exact same issue. Adding an object and trying to select it.

Enabling the Auto Rearrange Content option of the Array Controller made it for me.

Related

Cocoa binding NSTableView within NSTableView

I'm trying to display a NSTableView inside a NSTableView. This is for an iTunes-like albums/tracks view. So there's a list of 10 albums, each album has some tracks. I want to display the albums in the outer table view and the tracks for each album in the inner.
The first NSTableView is bound to an NSArrayController. Each object in the array has its own "tracks" NSArrayController, but I can't figure out how to tell the 'tracks' NSTableView that its content comes from a property of the 'album' NSTableView.
If I understand you right, the source content of the nested array controller comes from objectValue of the owner table cell. So you can't put the array controller content source to be the objectValue of the table cell. I'm doing similar in that I want to filter the array content based on the object value
What I'm doing, which seems to be working, is to make a seperate nib file for your nested table cell view, with it's own nstablecellview subclass. Include the array controller in the nib and create an outlet to it in your cell view subclass.
Register it with the table view in the viewDidLoad method of the tables view controller:
NSNib *cellView = [[NSNib alloc] initWithNibNamed:#"MyTableCellView" bundle:nil];
[myTableView registerNib:cellView forIdentifier:#"myTableCellView"];
Then, in the awakeFromNib method of your cell view subclass, manually make your bindings that require the object value:
[self.arrayController bind:#"contentSet"
toObject:self
withKeyPath:#"objectValue.tracks"
options:nil];
Voila.
Note that when using this technique, the files Owner of the nib file is not your nstablecellview subclass, it is the view controller of the table view.
The problem lies in not understanding the MVC-pattern (Model-View-Controller). Content for a view never comes from another view, it comes from model objects via a controller. The content of each of your tableviews always comes from an NSObjectController, or a subclass like NSArrayController. Basically, there are two solution:
bind the 'tracks' tableview to the selection of the 'album' array controller
create a 'tracks' array controller and bind it to the selection of the 'album' array controller. Bind the 'tracks' tableview to the 'tracks' array controller
A third solution is to use an NSTreeController with an NSOutlineView but outline views and tree controllers are notoriously hard to work with.

What is the simple way to bind an NSMutableArray to a single Colum NSTableView

Is there a simple way to bind a NSMutableArray made up of Strings to a Single column NSTableView without creating any new classes?
Make an NSArrayController in interface builder. Bind its Content Array to your mutable array, like so:
Then bind your table column to that array controller, like so:
(Note the self in the model key path field. That's what makes it work with an array of strings.)
Assuming you're using a view-based NSTableView, you must bind the entire table view (not the column) to the array controller's arrangedObjects. Then, bind the text field's value to the containing cell view's objectValue property.
This is documented here.
For cell-based NSTableViews, Amy Worrall's answer is the correct approach.

Lazy Fetching & Maintaining a sorted NSArrayController

I've got a sample project at
https://github.com/ericgorr/LazyFetching
The setup is pretty straightforward. In my CoreData model, I have two entities:
Shelf which has a name property and a to-many relationship (items) to the Item entity
Item which has a name property has a relationship to the Shelf entity (it's "parent")
I then have two NSArrayControllers:
Shelf Array Controller with the Mode 'Entity Name', the Entity Name of 'Shelf'. It Prepares Content and using Lazy Fetching.
Item Array Controller with the Mode 'Entity Name', the Entity Name of 'Item'. It does not prepare content and does not use lazy fetching. It's Content Set is bound to the Shelf Array Controller's selection and 'items' property.
On the Window, I've got two NSTableViews and two + buttons, each ties to one of the array controllers.
What I did was to:
Press the top + button and named the new Shelf entry 'a'
Press the top + button again and named the Shelf entry 'b'
Pressed the top column header to sort the Shelf entries in ascending order.
Make sure the 'b' entry is selected
Pressed the bottom + button and renamed the Item entry to anything
What I see happen then is the rows in the upper (Shelf) table change order so that the 'b' entry is listed first.
If I press the Info button, what it will do is:
NSLog the sort descriptors for the Shelf Array Controller
Print the names of the entries in the arrangedObjects array of the Shelf Array Controller
Call rearrangeObjects on Shelf Array Controller
Print out the names of the entries in the arrangedObjects array of the Shelf Array Controller
After all of this, I see, as expected, a single sort descriptor on the Shelf Array Controller: name, ascending, compare:
And the following output for arrangedObjects:
2013-05-19 16:08:56.023 LazyFetching[11791:303] b
2013-05-19 16:08:56.024 LazyFetching[11791:303] a
and
2013-05-19 16:08:56.024 LazyFetching[11791:303] b
2013-05-19 16:08:56.024 LazyFetching[11791:303] a
For some reason, it is no longer sorting the items in the Shelf Array Controller.
If I turn off Lazy Fetching for the Shelf Array Controller, everything seems to work correctly.
So, what I don't understand is why I am seeing this behavior with Lazy Fetching enabled. It seems possible that it is a bug. If so, I can certainly file what is likely to be a duplicate report. But, I am guessing, there is some good explanation and a standard way of dealing with it so items remain sorted by the sort descriptors in the arrangedObjects array of the array controller.
I found the only way I could force sorting after turning on lazy fetching for an NSArrayController (in IB) was to send a performSelector:withObject:afterDelay: in awakeFromNib with the delay set to 0:
(void)awakeFromNib {
[self performSelector:#selector(sort) withObject:self afterDelay:0.0];
}
(void)sort {
NSSortDescriptor* sorting = [NSSortDescriptor sortDescriptorWithKey:#""
ascending:YES];
[self.AC_Controller setSortDescriptors:[NSArray arrayWithObject:sorting]];
}
These methods were included in my main window controller where the NSArrayController resides in the associated nib file.
it seems that upon loading the nib containing the NSArrayController, the nib loading machinery invokes lazy fetching as per the IB setting but this overrides any sorting you may be trying to set programmatically or via IB in the NSTableView - by waiting for the next run loop the sorting will then "take" OK.
This code assumes you have an IBOutlet connected to the NSArrayController of course.

Can i separate the selection model from an NSTableView

Is there any way how i can maintain my own data model of selected items in a NSTableView.
I find it either pretty slow or complicated to keep the state of selected items when i update the table model.
You can maintain your own NSMutableIndexSet for the selected row indexes. If you're binding the table view's columns to an array controller's arrangedObjects and the array controller's contentArray to an array you own, bind the array controller's selectedIndexes to your index set. If you're implementing a data source, be the view's delegate as well and implement the delegate methods relevant to managing the selection.

Table view not updating according to bindings - Part Two

Title borrowed from this question, of which this one is not a duplicate. See my answer there for what was wrong for that questioner; I'm the author of that answer, and my problem is not that one.
I have a table view with three columns, whose Value bindings are bound to three properties of the arrangedObjects of an array controller. The array controller's contentArray is bound to the visitationResults of my document object; the items in that array are instances of a model class (VisitationResult). I have also bound the array controller's selectionIndexes and sortDescriptors to properties of my document.
I am mutating my property through a couple of accessors:
- (void) addVisitationResult:(VisitationResult *)newVisitationResult {
[self insertObject:newVisitationResult inVisitationResultsAtIndex:[self countOfVisitationResults]];
NSLog(#"arrayController arrangedObjects: %#", [arrayController arrangedObjects]);
}
That NSLog statement runs, and confirms that the array controller is gathering and arranging my model objects. This means that I am going through my property and getting KVO notifications for my document (which proves that the earlier questioner's problem, that of bypassing the property, is not the problem I'm having).
I added NSLog statements in my model object class's accessor methods. One of them is being called—by the array controller, in order to sort the objects (that property is the sort key). The other two, which the array controller doesn't know about, never get called.
Thus, my table view remains blank.
I found the problem: It's because I had explicitly bound the selectionIndexes and sortDescriptors bindings of the table view.
This wasn't necessary, anyway: I just checked, and the documentation says:
selectionIndexes
Typically, selectionIndexes is bound automatically to the NSArrayController that the first NSTableColumn is bound to.
sortDescriptors
Typically this binding is created automatically, binding to the sort descriptors of the NSArrayController of the initially bound NSTableColumn.
It appears that not only is it not necessary, but binding either or both of these two will break the table view.

Resources