I have a an NSMutableArray set up with objects that have a color, a bool value, and a name. I would like to have the array displayed in a table view with a color well depicting the object's color value, a check box for its bool value and then the name in the third column. I haven't had trouble setting up a table view with just a check box and name, however I am running in to some issues trying to get a color well to show up in the table.
I'm fairly certain I need to use a view-based table view instead of cell based table view, and that is where I am running into trouble. Am I supposed to use bindings to set up the table? If so how might I do that?
If not, what would be the correct way to set up a view-based table view?
I've tried using
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
and
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
However this doesn't seem to work. I've looked into using an NSArrayController, but I don't think that's what I need to implement this.
Any ideas?
Thanks!
I found an answer that worked.
I used a custom cell called LVColorWellCell (http://ringce.com/code/Cocoa/lvcolorwellcell) to implement a color well cell in a cell-based table view and it works great!
Related
I currently have a cell-based NSTableView where I need to add columns and bind them to my model. The text size for the columns is System-Small and everything works great because I can programmatically set the properties of the dataCell when I create the column.
With a view-based table and no dataCell, how can I make sure during the column creation that the text cell is the right size, etc? There does not seem to be a way to store a template column in the nib and load it multiple times (each time changing the identifier), before adding it to the table.
In switching to a view-based NSTableView, I understand that I can't bind a whole column on creation like I can with cell-based tables, but instead have to do it during:
-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
I can bind like this, but just need a way to pre-configure the appearance and layout of a column as I can't figure out how to even find the views within a new column as they don't have any identifiers like they would if they came from some sort of template.
I'm not sure if I am doing things right but this is my problem:
I have a view-based NSTableView using bindings to an arraycontroller.
I need to do some custom drawing on each row, depending the represented object as well as capture click in certain areas so for this I would need to have a controller for each row and set outlets for the sub-views in my custom cell view, but I don't understand how I can achieve this.
If I just add an object to the nib and make the connections to it, then I cannot tell which of the views is being drawn (or has been clicked).
You have to implement the delegate methods :
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
It's used by the table view to get a view for a give cell (column, row).
Then by using "makeViewWithIdentifier:owner:", you can get the a reusable cell with a given identifier and a given owner (view controller).
The simplest way is to design your cells in Interface Builder, and set a different identifier for each one. Then the method "makeViewWithIdentifier:owner" will automatically create a view for you for the given identifier.
I just found someone asked a similar question and the answer to it also satisfies my needs, so for anyone ending up here, this is what I did:
I set my NSTableCellView controller as the delegate of the NSTableView.
In my NSTableCellView subclass I implement the needed methods (drawRect:, mouseUp: and so forth) and call the respective methods in the controller.
To access the controller I get the NSTableView and then its delegate like this:
NSTableView *tableView = (NSTableView*)myView.superview.superview.superview;
MyControllerClass *controller = (MyControllerClass*)tableView.delegate;
[controller view:myView drawRect:dirtyRect]
On the controller, to tell which view is sending an event, I use their identifiers.
I'm creating a view-based table view and I'm having some trouble creating a custom group row. I want to have text in my group row and the default behavior Apple provides is great, but I need to change the color and appearance of the actual row. I have subclassed NSTableRowView and it works, but unfortunately not very well. Specifically when it starts to float I haven't figured out how to change the alpha of the view without making the text alpha change too.
It's actually pretty easy. Make a new subclass of NSTableRowView and override the method drawRect:.
It's best to always call [super drawRect:dirtyRect]; at the beginning.
Then you have the property self.isGroupRow. If it is, then just draw your custom group row.
You can load in the custom Row in the Table View delegate with the method tableView:rowViewForRow:.
Just return an instance of it right there
If I understand your question correctly, this solution should work for you:
Draw a custom NSTableRowView depending on if next row is group row
I've got a problem. [for which the only example I can find was shown during one of the WWDC 2011 presentations ("Maximising Productivity in Xcode 4"), but there is no source available (it was an app called Birdathon). Everything else I come up with is for iOS, and doesn't translate across.]
Basically, I have some view-based NSTableViews, and currently lay out the image / text fields within my NSTableCellView directly in the column. I've got a subclass of NSTableCellView which gives me the outlets to assign values to each of the text fields I use within that cell. The DataSource and Delegate are implemented and working fine - the TableView with my custom NSTableViewCell works fine.
My problem is I'd like to use the same cell in multiple different tables. Rather than have to recreate the same layout each time, I feel I should be able to draw the NSTableCellView just once in IB. [- and indeed, the Birdathon example I mentioned seemed to show the NSTableCellView being laid out in it's own NIB.]
I've found the answer for iOS in many places, here for example: How do you load custom UITableViewCells from Xib files?
Can anyone help me modify that for Cocoa on Mac?
Thanks,
David
Like this!
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView {
return count;
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSView *customView = [tableView makeViewWithIdentifier:#"customview"
owner:self];
…… // set properties
return customView;
}
In interface builder, set the identifier of your custom cell view to "customview" and it will automagically be created! Example:
Just replace "Automatic" with the identifier you are using
I am looking to make a Application and each row of the table view will have a column with a check box. What i am looking to do is... make the row that the checkbox is on go slightly see through if the check box on that row is checked (this should be implemented on every row of the table as every row has a checkbox) .
In your table view delegate, implement this method:
tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
From there you can get the checkbox button cell at the given row, and change the cell's drawing style based on it's state. Keep in mind though that cells are re-used, so you'll have to set the cell style for both checked and unchecked states.
I'm not too clear on what sort of drawing style you're aiming for. Try with a basic NSTextFieldCell first and if you can't accomplish what you're trying to do, create an NSCell subclass and handle the drawing code yourself.
Do you really mean "opaque"? ("Slightly opaque" is a lot like "slightly pregnant.")
If you mean change the color, add javascript to the checkbox to change the background of the div containing the checkbox. Here's an example.