An NSMatrix is vaguely similar to an NSTableView in that it allows you to arrange various cells in a spreadsheet-like table. NSMatrix works by using subclasses of NSCell and laying them out in rows and columns.
See Apple’s documentation at: http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSMatrix_Class/index.html#//apple_ref/doc/uid/TP40004060
If you need a matrix of image view’s that can be sorted by the user (similar to an outline view) then take a look at DragMatrix. —-
Answer: Each button is independent in a matrix. Use -cellAtRow:column: to access individual cells. —-
Answer: No. Any object in your nib file will be automatically allocated/initialized when that nib file is loaded. —-
Answer: Yes. It release all its cells on its own. However, you generally don’t need to deallocate things you loaded from a nib as the window controller will take care of releasing the nib’s contents when it gets deallocated. —-
A: There seems to be many ways for you to do that. If you just want to replace some cells you can use the NSMatrix method - (void)putCell:(NSCell *)newCell atRow:(int)row column:(int)column.
You can also try to create your NSMatrix programmatically with - (id)initWithFrame:(NSRect)frameRect mode:(int)aMode prototype:(NSCell *)aCell numberOfRows:(int)numRows numberOfColumns:(int)numColumns –EnglaBenny —- —- A2: I found that - (void)setCellClass:(Class)aClass doesn’t work because InterfaceBuilder creates the NSMatrix with a prototype cell instead of a class. You have to use - (void)setPrototype:(NSCell *)aCell instead.
To set a custom class for individual cells you just double-click the cell to select it and set the class on the Custom Class page of the Inspector for that cell. Once you have one cell of the correct class, you can set it as your prototype in - (void)awakeFromNib or wherever you need it. –John Dalbec —-
IIRC, you need to use the delegate to arrange this: since there’s actually only one cell involved; you need to change its color to match the location it’s placed at. Unfortunately, I can’t find what methods NSMatrix’s delegate will be sent. —- —- The delegate methods are documented in the NSControl class reference (NSMatrix’s superclass). –John Dalbec —-
Answer: Override -(NSColor *)highlightColorWithFrame:(NSRect)rect inView:(NSView *)controlView and return the color you want. —-
Answer: NSCells are not NSViews. Try obtaining the view with -controlView, then hide that. —-
Answer: Take the NSImageView template and drag it into your window (or view, or whatever). Then hold the option key and drag the corner out (the view will be converted to a matrix of image cells). This should work for any control that has a corresponding cell class. —-
I have an NSMatrix of NSButton cells with a scalable image on each cell, but I’m not able to get the images to scale as the NSMatrix resizes. I’m using the following code to set the image on the NSButton cell that is clicked:
- (void)setAutosizesCells:(BOOL)flag --zootbobbalu ----
Question (1/23/05): Can I set it so that an NSMatrix of NSButton cells has no selection capabilities whatsoever? This would mean that clicking on a cell would do nothing. Alternately, does anyone know how to disable NSButton cells without having them greyed out? (This is equivalent to no selection if all cells are disabled) –JediKnil
Question (6/21/2005): I’m trying to use a NSMatrix of NSImage cells with bindings. I create the matrix of NSImage cells. However, the NSMatrix bindings doesn’t have anywhere to place what the content object for the matrix should be. However, if I create a matrix of NSButton cells, the NSMatrix bindings has a place for contentArray. How I can have an NSMatrix use a NSArrayController as its content?
Answer (10/25/2007): I’m amazed that this question doesn’t come up more often! Binding a matrix of NSTextFieldCells to an array controller would seem like a fairly common thing, and the answer is neither obvious nor (as far as I can see) documented.
Answer: This is another one of those hidden features. First, place an object in your window that you want to be the component of your matrix. If you want a matrix of buttons, place a button. If you want a matrix of text fields, place a text field. Once you’ve done that, select your new object, hold down option, and resize it. Instead of actually changing the size, it will look like you’re creating copies. This changes the object into an NSMatrix, and the “copies” are actually the individual cells in the matrix. Answer 2: This doesn’t appear to work in IB3 (or I’m doing it wrong), but I’ve just found that if you select an object in your window you can then choose Layout->Embed Objects In->Matrix from the menu to wrap the object in an NSMatrix —- —- 8/16/2006 What about a custom class? I can’t see a way to create an NSMatrix of my own class that way. —-
Answer (12/5/2005): An NSMatrix is like a spreadsheet; you can select a single cell or discontiguous group of cells regardless of row/column, whereas NSTableView only allows selection of entire rows and entire columns. —-
Answer (01/04/2006): [ Short ] No. [ Longer ] No, not without serious subclassing which suggests rolling your own control would be best. —-
Answer: Can you describe what you mean by delete? It doesn’t make sense to completely remove a (row,col) point in a matrix without also removing the entire row and column. If you want to replace a cell with a fresh cell of some sort, you can use putCell:atRow:column:. —- Huh? Why doesn’t it make sense? Makes perfect sense to me, in certain circumstances. A matrix containing a set of “favourites” for example, where you decide you want to get rid of one. Probably countless other examples. –GC
Answer (10/25/2007):I don’t believe it’s possible to configure a matrix such that there’s no cell at a given location, but you can get the effect you’re looking for by replacing the existing cell with a new cell that doesn’t draw anything. You don’t need to create a custom NSCell subclass for this, just use NSCell itself. You’ll want to disable the new cell as well, so that the user doesn’t get feedback when they click in that spot. A bit of code follows. -CS
id blankCell = [[[NSCell alloc] init] autorelease]; [blankCell setEnabled:NO]; [matrix putCell:blankCell atRow:1 column:0];
Sorry to dig this dusty page up again, but…
Question (01/11/2007): Is it possible to alternate between two cell classes in a matrix’s rows? Specifically, a custom cell class on all odd rows, and an NSTextFieldCell on the even rows. Fortunately, my matrix instance only has one column.
Question (9/11/2007):I have each of cells of an NSMatrix set up to trigger the same action. Do I need to subclass NSMatrix to find out what cell was clicked? –Darknoon. —- Answer: No, the cell that was clicked is nearly always the selected cell. For some unusual cell/selection mode/user input combinations, it might in theory be the “key cell”, which is the cell that receives key events and is nearly always the same as the selected cell. Most of the time the selection is fine, especially if your action is easily undoable.
Please use the forums for further questions.