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
Related
I am developing a cocoa app where i need to create Menu and Submenu's in my application.
I have attached a screenshot designed using flex. How can i do the same in cocoa.
Any help would be appreciated.
Thanks.
Your question itself is incomplete,though will try to match the solution your expecting…The screenshot you posted(you never mentioned the source of the screenshot you have taken,by analysing the design i edited in your question as “Flex”) looks like you don’t want to deal with NSMenuItem and NSMenu classes for the drop down menus…
Solution 1: Make a custom View(probably subview of NSView like popview) that handles input, displaying labels,imageviews etc.
==> Basically,both the menu bar and menu item are wrapped to an NSView And the drop down menu is wrapped to an NSPanel…Well as per the design you have to use NSView,because you will be able to add the corner you like and yes,there is a possibility of adding back ground colour as well…The menu item actually has subviews of NSTextView. If its a menu bar item then it only has one text subview for its title, and if its a sub menu item then it has 3 text subviews, one for the check mark, one for the title and one for the hot key list…No need to worry about the handling the events,the respective classes do their own event handling…It’s quite a complicated solution,but matches your requirement…
Found some example for you,check out this code which is in C++.
Solution 2: NSTableView with custom cell.Could be fugly, but maybe worth a shot.
==> Create a custom NSTableCellView/NSCell,with NSImageView(for icons like Pen), NSTextView(for the text “Pen Thickness”) and one more NSImageView(for the right corner icon) as it's subviews …You have to perform one of the two actions when user hits your cell…(1) If you would like to have the submenu,then again that cell should create one more NSTableView using the origin (cell.frame.origin.x+cell.frame.size.width, cell.frame.origin.y)…(2) If there is no submenu,perform the direct task...
Example: Assume "MenuItemCell" is the custom class name,in the delegate method tableView
willDisplayCell add the cell...
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
MenuItemCell *cell = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
result.imageView.image = //ur image
result.textView.setString//;
result.imageView.image = //corner image icon,if you would like to have submenu upon clicking this cell.
return result;
}
On selecting the custom cell,
- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)rowIndex
{
NSLog(#"%i tapped!", rowIndex);
NSTableCellView *selectedRow = [tableView viewAtColumn:0 row:rowIndex makeIfNecessary:YES];
//if you would like to have the submenu,display one more NSTableView,based on the cell origin as i described above...don't forget to add the animation..
return YES;
}
Happy Coding.. :-)
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 am trying to make a drill down table with storyboards in a Tab bar app.
I am having a problem with working out how to get each row in a main table to point to other different tables.
This is the code I have used to detect row selection.
-(void)tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *__strong)indexPath {
TableViewController *table = [self.storyboard instantiateViewControllerWithIdentifier:#"table"];
[self.navigationController pushViewController:table animated:YES];
}
How do I proceed please?
The real beauty to UIStoryBoards are the segues.
1. You can link one prototype cell push segue to another different table view controller
2. You can link one prototype cell push segue loop back to itself for drill downs.
[self performSegueWithIdentifier:#"segueID" sender:MyObject];
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:#"segueID"]) segue.destinationViewController.chosenCell = sender;
}
If the segue is from the cell click, the sender is that of the cell and you can get it's indexpath from the table, and pass new information to the upcoming view controller.
If you want the cell to take various paths when clicked, keep the didSelectRowAtIndexPath and call performSegueWithIndetifier so you can choose left or right.
Tutorial: http://jleeiii.blogspot.com/2012/05/uistoryboard-power-drill-batteries.html
GitHub: http://www.github.com/jllust/UIStoryboardExamples
I was extremely frustrated with this, as the storyboards with tableviews is really not covered ANYWHERE. I literally almost pulled my hair out trying to figure this out. So my frustration is your luck!
Comparing your code to mine, yours looks fine other than the fact that in the initial didSelectRowAtIndexPath: method, you have *_strong, which should not have the _strong part. It should just be like this: (NSIndexPath *)indexPath {
Additionally, be careful in when you are referring to the instantiateViewControllerWithIdentifier in Interface Builder. I accidentally filled out the name of the instance variable under 'TITLE' instead of 'IDENTIFIER'
I hope this helps. I completely understand your frustration.
In your storyboard, if the content is Static Cells, you can ctrl-drag from the specific row to a Navigation Controller. Then connect the Navigation Controller to your next table. Look at this screen shot. http://cl.ly/1V2y0v202y390U0A351x
My guess is that progromatically, you'll need to do something similar, segue to a NavigationController which has a table view controller as it's root view.
I have an view-based TableView with an image and a NSTextField. I've got some links inside my NSTextField and I have tried many options (http://developer.apple.com/library/mac/#qa/qa2006/qa1487.html, dsclickableurltextfield) but nothing works out, because it seems that these options only fit to an cell-based tableview. I also watched the wwdc 2010 cocoa tips and tricks with an good explanation for links inside tableviews. But the custom NSTextFieldCell doesn't work for me. The mouse-events don't reach the custom class and for that reason nothings happens...
I hope you've got the right idea to solve this problem...
You need to add this to your table view's delegate:
- (BOOL)tableView:(NSTableView *)tableView shouldTrackCell:(NSCell *)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
return YES; // required for specific tracking control in our NSTextFieldCell
}
i am having a problem displaying context sensitive menu on control click on a tableview when multiple rows are selected.
Its working fine when a single row is selected and then control clicked on it.
The way i am implementing this is shown below:
-(void)doSingleClick
{
NSLog(#"single clicked");
if([[NSApp currentEvent] modifierFlags] & NSControlKeyMask)
{
NSLog(#"control clicked.......");
[NSMenu popUpContextMenu:[self showContextMenu] withEvent:[NSApp currentEvent] forView:tableView];
return;
}
}
and showContextMenu function returns a NSMenu object.
I am dong it this way as my table view for some strange reason does not recognize mouseDown or mouseUp or menuForEvent events.
the problem with the above code segment is, when multiple rows are selected and control clicked, it does not recognize the control click and does not go into that loop and hence not displaying the context menu.
Please suggest me a mechanism to achieve this.
Thanks
I don't recommend the approach that is given in the answers above. Instead, look at the "DragNDropOutlineView" example in Leopard and higher. That, and the release notes, give a proper way to implement contextual menus for a single row, or multiple rows. This includes having AppKit automatically do the proper highlighting.
corbin dunn
(NSTableView Software Engineer)
i hve tableviewcontroller class which is a subclass of NSTableView.
That's very bad naming and suggests that you are not architecting your application properly. Views aren't controllers. Keep them separate.
but this class in which i implemented menuForEvent method but its not getting called for some reason.
Did you make your table view an instance of this class in Interface Builder? If not, your instance is still an NSTableView, and the subclass you wrote is what Ian Hickson might call “a work of fiction”.
Corbin's answer is the best one here.
link text
I don't believe the action method is called when multiple rows are selected.
What would probably be a lot easier would be to override the menuForEvent: method in NSTableView. You'd have to create a subclass of NSTableView to do this, but it would be a cleaner solution.
You could also create an informal protocol (a category on NSObject) and have the NSTableView delegate return the appropriate menu.
#interface NSObject (NSCustomTableViewDelegate)
- (NSMenu *)tableView:(NSTableView *)tableView menuForEvent:(NSEvent *)event;
#end
#implementation NSObject (NSCustomTableViewDelegate)
- (NSMenu *)tableView:(NSTableView *)tableView menuForEvent:(NSEvent *)event {
return nil;
}
#end
And in your NSTableView subclass:
- (NSMenu *)menuForEvent:(NSEvent *)event {
return [[self delegate] tableView:self menuForEvent:event];
}