Showing serial nos. in a table column using NSArrayController and cocoa bindings - cocoa

I have a simple question:
How to show serial nos. or (row no. +
1) in a table column using cocoa bindings and array controller?
I have made an application using cocoa bindings and array controller, in which I am displaying names of certain persons in a table column. The class from which I am displaying is named as: Person. Now I want to show serial nos. in first column, such that list gets displayed like this :
1 / John
2 / Peter
It is very easy to do if I use data source method:
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
I just need to return something like this :
return [NSString stringWithFormat:#"%d",rowIndex+1];
but I am not getting how to do this via cocoa binding and array controller.
Can anyone suggest me solution for it?
Thanks,
Miraaj

One way you can do this is to actually mix using bindings and a data source with the same table view. Go ahead and hook up bindings for the rest of your table columns, but leave the one you want to display the indices in unbound. Then, just implement the table data source as normal for just that one column. It should only ever ask you for data fir Amy unbound columns, and just use the bound data for the rest.

Related

Stop Core Data objects from auto-arranging

I have a NSTableView and I binded my Array Controller to it. The Array Controller is binded to my Managed Object Context. Everything in the NSTableView works but the objects in my TableView rearranges randomly when I start up my app. For example, the order of [1,2,3] will suddenly change to [3,1,2] and this happens occasionally. I want it so that things stay in the order they were put when making them. How do I stop the rearranging?
The rows in the CoreData data store are not sorted! If you want to view them by creation date you need to add your own timestamp to every object and pass a sort descriptor to the fetch request.
Edit: If you are using SQLite as your CoreData store I recommend to have a look at the SQLite file. This will give you an idea on how CoreData works and explain why certain things (like unordered rows) are the way they are.
P.S. I use Base to inspect SQLite databases. It's not cheap but quite nice.

Core data, bindings, NSArrayController and table views - how to generate a view of a core data context

I have a working system that lets me build a database containing instances of various entities , all linked together nicely.
Before I knew I would care, I came across a tutorial on using Core Data and bindings, and it went through a complete case where you get a table showing all the entities of some type with a column for each property. It showed both the UI side and the Data model side - not that I need the data model part at this point. Now, darned if I can find it. This is one of those things that is supposed to be easy, and requires virtually no code, but getting exactly the right connections in UIBuilder is not going to happen if I can't find instructions.
Also, I thought I came across an example of something like a query editor where the user could select which properties to sort on, which to match on, etc. Did I imagine that?
Anyone out there know where I can find such?
Sure, you can do this without code:
Add an array controller to your nib.
Bind or connect an outlet for its managed object context
Set the array controller to Entity mode, fill in the entity name, and select Prepares Content.
Bind your table view columns to array controller's arranged objects, and fill in the key name for the model key.
Regarding the query editor, open up the model, and on the Editor menu click Add Fetch Request.
I found at least a partial answer to the query editor question, in this apple tutorial. Not sure how far it will get me, as I prefer to write code where possible, since then I can leave a trail of comments.

Custom NSTableViewHeaderCell

I've created a custom NSTableViewHeaderCell class but I still have one issue with separators: How know if I am drawing the last column header or not?
Actualy I don't want to draw a separator for the last column but I didn't found a way to know it is the last one.
Thanks for your help
Since you're creating a subclass of NSTableHeaderCell, create one more property on it for a NSTableColumn. You can set this property as you create and set an instance of your subclass for each of your table's columns.
Now that your subclass instances know the table columns to which they belong, they can use this to determine the table view. From the table view, you can get an array of table columns, and if you compare a given table column to the lastObject value of that array, you should be able to tell if your header cell belongs to the last table column.
If you choose to enable reordering for your table columns, you may have some more coding to do, but this should get you started.
Good luck to you in your endeavors.

Data is not displayed in the table

I have created a small application,in which i have draged and droped a NSTableView onto the window and binded to an datasource, in the datasource i have created NSMutableArray and added 2 records to it. And also defined methods numberOfRowsInTableView and rest of the methods.
When i run the application it is showing the rows but the data is not displaying.
So. You've connected your table view's datasource outlet to some controller object that adopts NSTableDataSource protocol.
You mention numberOfRowsInTableView, which tells the table how many rows it should expect but lump together the "rest of the methods." One of those other methods is exactly responsible for showing the data for a given row and column.
What seems to be happening is this:
Table: "How many rows we got here?"
Data Source: "Three."
Table: "Gimme the object to display at
row 0, column 0."
Data Source: [ sound of crickets
chirping ]
You should probably post the exact code you're using for your implementation of the tableView:objectValueForTableColumn:row: method. Remember to include the method signature itself, given its importance in being recognized as the selector the protocol is looking for.

NSTableColumn binding using Collection Operators like #sum

Mac OS X. CoreData app. NSTableView controlled by NSArrayController bound to managed object context for the Country entity. The Country entity has a 'name' attribute and a to-many relationship, 'branches', to a Branch entity. The Branch entity has a 'sales' attribute (an NSNumber).
The NSTableView has two NSTableColumns. The first shows the name of the Country. The second should show the total sales for that Country across all its Branches.
The first column's value is bound to the NSArrayController's arrangedObjects with a model key path of 'name'. No problem there.
The second column's value is bound to the NSArrayController's arrangedObjects with a model key path of 'branches.#sum.sales'. This doesn't work. I get the error message: " addObserver:forKeyPath:options:context:] is not supported. Key path: #sum.sales"
If, instead, I add a 'totalSales' method to my Country class and the method is implemented as follows:
- (NSNumber *)totalSales
{
return [[self branches] valueForKeyPath:#"#sum.sales"];
}
and I then bind the column to 'totalSales' it works fine. My understanding of the Collection Operators documentation is that this should be the same as simply binding to 'branches.#sum.sales'. I can't see why the latter doesn't work. Any ideas? I have seen similar questions in this and other forums, but have yet to see an explanation or solution.
I don't know if this is still topic for you, but it surely does need an answer.
The second column's value should be bound to NSArrayController in exactly the sam way as first. I don't know why you made it differently and what actually you wanted to achieve.
Your first task was to bind table columns to array columns and this works the same way for all the columns and types.
Second task is to get sum of certain NSTableColumn bound to certain other object, like NSTextfield. And this is done like this:
[totalCountField bind: #"value" toObject: arrayController
withKeyPath:#"arrangedObjects.#sum.price" options:nil];

Resources