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.
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.
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.
Given an NSArrayController "objController" which is bound to the moc in IB, shouldn't the following work? And given that it doesn't seem to work, how do I go about retrieving an entity's name from my NSArrayController?
for (NSManagedObject *thisObj in [objController arrangedObjects])
{
NSEntityDescription *description = [thisObj entity];
NSString *entityName = [description name];
// do something with entityName...
NSString *entityAttributeValue = [thisObj valueForKey:#"attributeKey"];
// do something with entityAttributeValue...
}
The "objController" is IBOutlet-ed and set to "Entity Name" Mode, with the entity name set to an entity defined in the model. This entity does have child entities (and thus the reason I would like to access its description name, since the NSArrayController could store many different child entity types), but the presence of child entities doesn't seem to make a difference anyway.
... in the debugger, it looks like "description" is a valid NSEntityDescription object, but "entityName" gets set to a _PFEncodedString object, with no content. However, "entityAttributeValue" is just fine, populated with the correct value stored in Core Data.
One way around this, I suppose, would be to custom-class all my CD entities, then use -isKindOfClass to get the information I need.
Any ideas?
_PFEncodedString is a (private) concrete subclass of NSString and can therefore be used as any other NSString.
I want to dynamically hide/show some of the columns in a NSTableView, based on the data that is going to be displayed - basically, if a column is empty I'd like the column to be hidden. I'm currently populating the table with a controller class as the delegate for the table.
Any ideas? I see that I can set the column hidden in Interface Builder, however there doesn't seem to be a good time to go through the columns and check if they are empty or not, since there doesn't seem to be a method that is called before/after all of the data in the table is populated.
In Mac OS X v10.5 and later, there is the setHidden: selector for NSTableColumn.
This allows columns to be dynamically hidden / shown with the use of identifiers:
NSInteger colIdx;
NSTableColumn* col;
colIdx = [myTable columnWithIdentifier:#"columnIdent"];
col = [myTable.tableColumns objectAtIndex:colIdx];
[col setHidden:YES];
I've done this with bindings, but setting them up programmatically instead of through Interface Builder.
This psuedo-snippet should give you the gist of it:
NSTableColumn *aColumn = [[NSTableColumn alloc] initWithIdentifier:attr];
[aColumn setWidth:DEFAULTCOLWIDTH];
[aColumn setMinWidth:MINCOLWIDTH];
[[aColumn headerCell] setStringValue:columnLabel];
[aColumn bind:#"value"
toObject:arrayController
withKeyPath:keyPath
options:nil];
[tableView addTableColumn:aColumn];
[aColumn release];
Of course you can add formatters and all that stuff also.
It does not work in the Interface Builder. However it works programatically. Here is how I bind a NSTableViewColumn with the identifier "Status" to a key in my NSUserDefaults:
Swift:
tableView.tableColumnWithIdentifier("Status")?.bind("hidden", toObject: NSUserDefaults.standardUserDefaults(), withKeyPath: "TableColumnStatus", options: nil)
Objective-C:
[[self.tableView tableColumnWithIdentifier:#"Status"] bind:#"hidden" toObject:[NSUserDefaults standardUserDefaults] withKeyPath:#"TableColumnStatus" options:nil];
I don't have a complete answer at this time, but look into Bindings. It's generally possible to do all sorts of things with Cocoa Bindings.
There's no Visibility binding for NSTableColumn, but you may be able to set the width to 0.
Then you can bind it to the Null Placeholder, and set this value to 0 - but don't forget to set the other Placeholders to reasonable values.
(As I said, this is just a start, it might need some tweaking).
A NSTable is just the class that paints the table. As you said yourself, you have some class you give the table as delegate and this class feeds the table with the data to display. If you store the table data as NSArray's within your delegate class, it should be easy to find out if one column is empty, isn't it? And NSArray asks your class via delegate method how many columns there are, so when you are asked, why not looking for how many columns you have data and report that number instead of the real number of columns you store internally and then when being asked for providing the data for (column,row), just skip the empty column.
There is no one time all the data is populated. NSTableView does not store data, it dynamically asks for it from its data source (or bound-to objects if you're using bindings). It just draws using data it gets from the data source and ditches it. You shouldn't see the table ask for data for anything that isn't visible, for example.
It sounds like you're using a datasource? When the data changes, it's your responsibility to call -reloadData on the table, which is a bit of a misnomer. It's more like 'invalidate everything'.
That is, you should already know when the data changes. That's the point at which you can compute what columns should be hidden.
#amrox - If I am understanding your suggestion correctly, you're saying that I should bind a value to the hidden property of the NSTableColumns in my table? That seems like it would work, however I don't think that NSTableColumn has a hidden property, since the isHidden and setHidden messages control the visibility of the column - which tells me that this isn't a property, unless I'm missing something (which is quite possible).
I would like to post my solution updated for Swift 4 using Cocoa bindings and the actual isHidden flag without touching the column widths (as you might need to restore the original value afterwards...). Suppose we have a Checkbox to toggle some column visibility (or you can always toggle the hideColumnsFlag variable in the example below in any other way you like):
class ViewController: NSViewController {
// define the boolean binding variable to hide the columns and use its name as keypath
#objc dynamic var hideColumnsFlag = true
// Referring the column(s)
// Method 1: creating IBOutlet(s) for the column(s): just ctrl-drag each column here to add it
#IBOutlet weak var hideableTableColumn: NSTableColumn!
// add as many column outlets as you need...
// or, if you prefer working with columns' string keypaths
// Method 2: use just the table view IBOutlet and its column identifiers (you **must** anyway set the latter identifiers manually via IB for each column)
#IBOutlet weak var theTableView: NSTableView! // this line could be actually removed if using the first method on this example, but in a real case, you will probably need it anyway.
// MARK: View Controller Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
// Method 1
// referring the columns by using the outlets as such:
hideableTableColumn.bind(.hidden, to: self, withKeyPath: "hideColumnsFlag", options: nil)
// repeat for each column outlet.
// Method 2
// or if you need/prefer to use the column identifiers strings then:
// theTableView.tableColumn(withIdentifier: .init("columnName"))?.bind(.hidden, to: self, withKeyPath: "hideColumnsFlag", options: nil)
// repeat for each column identifier you have set.
// obviously use just one method by commenting/uncommenting one or the other.
}
// MARK: Actions
// this is the checkBox action method, just toggling the boolean variable bound to the columns in the viewDidLoad method.
#IBAction func hideColumnsCheckboxAction(_ sender: NSButton) {
hideColumnsFlag = sender.state == .on
}
}
As you may have noticed, there is no way yet to bind the Hidden flag in Interface Builder as on XCode10: you can see the Enabled or Editable bindings, but only programmatically you will have access to the isHidden flag for the column, as it is called in Swift.
As noted in comments, the second method relies on the column identifiers you must manually set either via Interface Builder on the Identity field after selecting the relevant columns or, if you have an array of column names, you can enumerate the table columns and assign the identifiers as well as the bindings instead of repeating similar code lines.
I found a straightforward solution for it.
If you want to hide any column with the Cocoa binding technology:
In your instance of the NSArrayController, create an attribute/parameter/slot/keyed value which will have NSNumber 0 if you want a particular column to be hidden and any value if not.
Bind the table column object's maxWidth parameter to the data slot, described in (1). We will use the maxWidth bound parameter as a message receiver.
Subclass the NSTableColumn:
import Cocoa
class Column: NSTableColumn {
/// Observe the binding messages
override func setValue(_ value: Any?, forKey key: String) {
if key == "maxWidth" && value != nil { // Filters the signal
let w = value as! NSNumber // Explores change
if w == NSNumber(integerLiteral: 0) {
self.isHidden = true
} else {
self.isHidden = false
}
return // No propagation for the value change
}
super.setValue(value, forKey: key) // Propagate the signal
}
}
Change the class of the column to Column.