Cocoa - Displaying Nested Arrays in a Table View - cocoa

I have a tree controller bound to an array, called "content". "content" is an array of model objects, called "Car". Each "Car" contains an NSString called "carName" and an NSMutableArray called "mostPopularColors". "mostPopularColors" contains NSMutableDictionary objects with keys like: "most popular", "second most popular", "third most popular" etc and values like: "red", "green", "blue" etc.
An outline view is bound to the tree controller arranged objects and displays the "carName" of every "Car" in "content". A separate table view lists every "carName" in one column. This is done by having an array controller bound to the tree controller (controller key: selection, model key path: allChildLeafs).The table column value is then bound to the array controller's arranged objects, model key path: carName.
In the table view, I want two other columns listing the most popular color and second most popular color respectively. So the final table should have three columns listing all the car names along with each car's two most popular colors.
I can access the car names as described but not the colours since they themselves are in arrays.
I have tried to make a second array controller and link it to the first but can't get it to work.
So in the end I want to be able to select a car or cars in the outline view and see all their names and top two colors of each in the table view.

It seems the second array controller did not work because it isn't possible to connect two array controllers to one table view.
The array controller that was bound to the tree controller (controller key: selection, model key path: allChildLeafs) was left in place but not bound to any view.
In Xcode an IBOutlet NSArrayController was created and then connected to a newly created array controller in Interface Builder (IB). Also, an new NSMutableArray was declared, with setter and getter methods. Then, the following code was used to bind the array controller to the new NSMutableArray:
[newArrayController bind:NSContentArrayBinding toObject:self withKeyPath:#"mutableArray" options:nil];
So now the array controller would "hold" whatever was in the new mutable array. The contents of the array could be displayed in a table view by connecting the new array controller to a table view.
All that was needed was to make this mutable array contain an NSMutableDictionary object for each car. Each dictionary would have three key value pairs. The three keys would be: "carName", "mostPopularColor", "secondMostPopularColor".
Since the old array controller held the array of "Car" objects currently selected in the outline view, this was done by first getting that array of "Car" objects. To do this, changes in the old array controller's arrangedObjects were observed and the new array of "Car" objects were observed using:
[oldArrayController addObserver:self forKeyPath:#"arrangedObjects" options:NSKeyValueObservingOptionNew context:nil];
To handle the observation and use the new array of "Car" objects to get the final array of dictionary objects the following method was implemented:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {if (object == selectedChildLeafsController)
{
if ([[object arrangedObjects] count] > 0)
{//make a new mutable array, here called "array", of dictionaries from your array of "Car" objects which is found in [object arrangedObjects] . And then something like...
[self setMutableArray: array];
[newArrayController bind:NSContentArrayBinding toObject:self withKeyPath:#"selectedBonds" options:nil];}else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}}

Related

NSArray ArrayController SelectedObjects nil

I have a coredata app with an Entity called Product.
In my interface I have an array controller for this entity called ProductAC. I have bound a table view and text fields to the array controller and can see the details, and I can add objects to it.
Now I want to create an export file for some of the objects. In the tableview I can select them, and then in the code I have this:
NSArray *uploadProducts = [ProductAC selectedObjects];
NSEnumerator *loop = [uploadProducts objectEnumerator];
This produces a nil value for the uploadProducts array.
How do I select the items to further process them? I have cleaned the file, closed and re-opened Xcode but I cannot seem to 'grab' the selected objects from the tableview?
thanks
Make sure you have the table view selectionIndexes property bound to the corresponding property on your array controller.

Cocoa Bindings for hierarchical model

I have a NSCollectionView based master-detail interface,
where I want to display Boards in the master and Lists+Cards in the detail view.
Board, holds a NSMutableArray property lists of type List
List, holds a NSArray property cards of type Card
Card, has a NSString property name
The relationship is thus Board --> to-many List --> to-many Card
The master interface is fine.
The detail interface gets populated with corresponding Lists' titles
for a Board. Within the detail interface I also want to populate a NSPopupButton with the
cards for every list.
Problem: the NSPopupButton is empty.
Output: [<__NSArrayI 0x60000007b240> addObserver:forKeyPath:options:context:] is not supported. Key path: name
So after reading KVO, KVC and the Bindings documentation I am not sure if I need to do manual KVO for this sort of hierarchical model. Also the output hints that the name property is not KVC/KVO compliant, but it's just a NSString?
Do you suggest using an NSTreeController for this?
Bindings are setup like so:
BoardArrayController -> bound to File's owner
** Model key path: boards
ListArrayController -> bound to BoardArrayController
** Controller key: arrangedObjects
** Model key path: lists
** Mode: Class
CardArrayController -> bound to ListArrayController
** Controller key: arranged Objects
** Model key path: cards
** Mode: Class
The NSPopupButton has
Controller key for Content: arrangedObjects
Controller key for Content Value: arrangedObjects and model key path: name
Suggestions please
If I understand properly, in the master interface, the user selects a Board. Then, the detail interface should show the selected Board's lists. If so, the ListArrayController should be bound to BoardArrayController, controller key selection (not arrangedObjects), model key path lists.
Similarly, the CardArrayController should be bound to ListArrayController, controller key selection, model key path cards. Although it's not clear to me if the user has to first select a List and then sees a pop-up with that List's cards or if the pop-up is present in each item in the second collection view. If that's the case, then you'll need a separate array controller for each item, which is easiest if the item view is in a separate NIB.
If each list is the representedObject for each view item in the collection view, then you can populate each popupButton with a readonly NSArray property dependent upon the cards array that is in each list. In the List class add arrangedCards as a property.
- (NSArray *)arrangedCards
{
return [[self valueForKey:#"cards"] sortedArrayUsingDescriptors:
[self arrangedCardsSortDescriptors]];
}
Use the sort you want for the popup. This arranges by name.
- (NSArray *)arrangedCardsSortDescriptors
{
NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:
#"name" ascending:YES];
return #[sortByName];
}
Bind the Content of the popup to the NSCollectionViewItem.
Model Key Path is representedObject.arrangedCards.
Use representedObject.arrangedCards.name as the Content Values.

How can I bind to NSTableColumn's headerTitle?

I would like to bind NSTableColumn's headerTitle property to an NSMutableArray in my model layer (via an NSArrayController).
Basically I want to have an array where I can change values and have the table column header titles update. Is that reasonable?
However, the headerTitle binding wants an single NSString and I'm not sure how to connect my model object to this binding via my NSArrayController. Google does not give many hits for this problem.
My model layer consists of two class (both of which are appropriately KVC compliant). The first is a model which represents a single column title, it has one property title,
// A model class representing the column title of single NSTableColumn
#interface ColumnTitle : NSObject
#property NSString *title;
+ (ColumnTitle*) columnTitleWithTitle:(NSString*) aString;
#end
The second a model object which represents an ordered group of ColumnTitle objects,
// Class representing an order collection of model items
#interface TableColumnTitles : NSObject
#property NSMutableArray* columnTitles; // an array of ColumnTitle objects
// These are the KVC array accessors
-(void) insertObject:(ColumnTitle*)columnTitle inColumnTitlesAtIndex:(NSUInteger)index;
- (void)removeObjectFromColumnTitlesAtIndex:(NSUInteger)index;
- (void)replaceObjectInColumnTitlesAtIndex:(NSUInteger)index withObject:(ColumnTitle*)columnTitle;
#end
Note that TableColumnTitles object implements the above array accessors which are required for the bindings. Any suggestions?
Haven't tried that before but what you're actually asking for is using KVC for array indexes. A quick google didn't turn up anything on that issue except some results that indicate it's not (yet) possible (check this)
The easiest work-around I could come up with would be to simply add dedicated properties for the array indexes.. not nice but does the job.
So for a NSMutableArray called myArray and contains objects with title properties of type NSString you'd do something like:
#property (nonatomic, readonly, getter = columnOneGetter) NSString *columnOneString;
(NSString*) columnOneGetter
{
return myArray[0].title;
}
Always assuming of course their number is known in advance and we're not talking 200 columns :-)
I think this may/may not be what you're after, but quick google search landed me here:
http://pinkstone.co.uk/how-to-add-touch-events-to-a-uitableviewfooter-or-header/
edit: i realize this is for mac (not ios) but should be pretty easy to translate if it actually helps.

How can NSArrayController sort on a to-many relationship?

Using CoreData, I have an entity "Bookmark", that has an to-many relationship named 'tags' to another entity "Tag", and some commun attributes (string, date, ...).
In a NSTableView we display the Bookmarks entity via Binding:
the NSArrayController is binded to File's Owner.managedObjectContext
(standard XCode CoreData template, the managedObjectContext is in the AppDelegate)
The columns in TableView are binded to their respective attribute. In particular the Tag column is binded to this arrayController.arrangedObjects.tags with a subclass of NSValueTransformer so that we can show, as an NSString, a summary of the to-many relationship.
It work. Now when I click on column header the whole table view get sorted correctly except for the 'tag' column where I get this:
-[_NSFaultingMutableSet compare:]: unrecognized selector sent to instance
For sure the "Set" from this to-many relationship doesn't respond to the selector 'compare:'.
Question:
How can I make this work ? How can I sort on a to-many relationship ?
Are something like the ValueTransformer available ? If I could supply a custom class that would do the compare: for the ArrayController to know...
One possible hack: since _NSFaultingMutableSet is a NSSet, we can add the selector 'compare:' via a categorie.
#interface NSSet (someAdditions)
- (NSComparisonResult)compare:(NSSet *)anotherSet;
#end
#implementation NSSet (someAdditions)
- (NSComparisonResult)compare:(NSSet *)aSet {
...
}
#end
we can now implement this compare: selector as we wish, like comparing the count of each set, or their NSString representation in some way.
It work in my App. I re-enabled the 'Creates Sort Descriptor' on the binding of the NSTableColumn and can now click on the header of my tableView to sort.
It's a hack because it affect all NSSet... But at least I have my hook.
What do you think ?

How do you Sort Bound Data in NSTableColumn using InterfaceBuilder binding?

Sorting Bound Data in NSTableColumn using IB binding.
Keys: NSTableColumn , sorting, NSArrayController, Content Set
A contentSet serves like a data source for a TableColumn
This deals with a SplitView with two single column NSTableViews
The names of the TableViews are BookCategory and
Books.
The Books Table has a single column with book_titles.
The Class BookCategory has a one to many relationship
to Book.
The BookCategory Table is sorted at load using:
#implementation MyBookCategoryController
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
NSSortDescriptor *descript =
[NSSortDescriptor sortDescriptorWithKey:#"name"
ascending:YES selector:#selector(caseInsensitiveCompare:)];
[self setSortDescriptors:[[NSArray arrayWithObject:descript] autorelease] ];
}
return self;
}
This same approach fails to sort the BookTitle table at load. !!
The BookTitle table/column loads unsorted.
For the TableColumn the Attributes Inspector has
Sort Key:title
Selector: caseInsensitiveCompare:
Order: Ascending
This appears to enable the sorting when one clicks
on the column header.
I want the data sorted when the view loads.
The binding inspector for this book_title column has:
Value : BookCategoryArrayController.arrangedObjects.name
The BookTitleArrayController in binding inspector shows
Content Set: Book Category ArrayController.selection.books
To restate the problem, the tableview with the book titles
load unsorted. It sorts only after the user's FIRST click on the
column header.
Say there are three book categories Art, History, Sports.
When the app loads the left table in the splitview is sorted,
that is :
Art
History
Sports
When the user selects ANY category, titles for all books
in the category appear in the right tableView but unsorted.
If the user clicks on the Header of the book_title TableColumn
the initial sorting is done on the column. Thereafter
the selection of ANY book category causes a sorted display
of book_titles in the right tableView. That is, ONLY the first
selection of a category results in an Unsorted book title list.
Thanks a lot for reading, Mark
This is an outline of what finally worked for me.
Sorting with CategoryArrayController (NSArrayController)
Bindings Inspector
Content Set Bind to: CategoryArrayController.selection.books
SortDescriptors Bind to: ShareUserDefaultsController
Controller Key: values
sortDescriptors (note: exclamation point)
NSUnarchiveFromData
Shared User Defaults Controller
Referencing Outlets
userDefaultsController -- AppController (C:AppController)
Referencing Bindings
values.sortDescriptors -- Sort Descriptors Rx Array Controller (C:NSArrayController)
-- Sort Descriptors Category Array Controller (C:NSArrayController)
Clicking on Category Header does no sort. Selects all in Cats, and empties Recipe Table
Comments on the above welcome.
TableViewCat (NSTableController)
Attributes Inspector
TableView
Selection
Empty,Column,Type Select
Control
State.Enabled
Connections Inspec
None
Bindings Inspec
None
TableColumn -Category (NSTableColumn)
Attributes Inspec
Sort Key : None
Bindings Inspec
Value -- Category Array Controller.arrangedObjects.name
Sorting with RxArrayController (C:NSArrayController)
Attributes Inspec
Array Controller: 1-5 of 6
Object Controller
EntityName: Recipe
Prepares Content
Editable
Connections Inspector
Referencing Bindings
arrangedObjects.name -- Value - Table Column Recipe
selectedObjects -- Double Click Argument - Table View Book
Bindings Inspector
Controller Content
Content Set
Category Array Controller.selection.books
Controller Content Parameters
Sort Descriptors Bind to: ShareUserDefaultsController
Controller Key: values
sortDescriptors (note: exclamation point)
NSUnarchiveFromData
Parameters
Managed Object Context(App Delegate.manangedObjectContext
TableView Book (NSTableView)
Attributes Inspector
TableView
Selection Empty,Column,Type Select
Control
State.Enabled
Connections Inspec
Bindings
DoubleClick Argument -- Book Array Controller.selectedObjects
Double Click Target -- App Delegate.self
Bindings Inspec
Double Click Argument (Book Array Controller.selectedObjects)
Double Click Target (App Delegate.self)
Bind To: App Delegate
Model Key Path: self (Repeats ?)
Selector Name: newBookAction:
Conditionally Sets Enabled
TableColumn - Book (NSTableColumn)
Attributes Inspec
Sort Key : None
Connections Inspec
Bindings
Value -- Book Array Controller.arrangedObjects.name
Bindings Inspec
Value -- Book Array Controller.arrangedObjects.name

Resources