View Full Version : Cocoa: Programmmatic creation of an NSMatrix.

Jan 18, 2007, 08:23 AM
I am making an application, in which I wan tto somehow use dynamic NSMatrices. Let's just say that I have an NSArray that will hold some NSStrings inside it. We don't know the exact size of the array, so it's impossible to make the NSMatrix with Interface Builder.

The cells will just be switch buttons (although I don't think that this matters to my question so much).

My question is: How can I programmatically create an NSMatrix just using an array of nsstrings (an knowing that the NSbuttonCells will be of the switch buttons size).

I know there are functions that allow me to add and remove cells, but applying them to my code has absolutely no effect. Can anyone give me a brief example?

Jan 18, 2007, 08:28 AM
I do something similar in PosterPaint (although with images). You can download the source but I can't remember if this is in the shipping version of the much delayed next version! It's used in the Backgrounds and Grid preference pane, but the actual code is in the UI Items framework...

I'll look at the code when I get home.

From my memory I create the Matrix in IB then manipulate it at runtime by altering the number of rows/columns and resizing the cells as required...

Jan 19, 2007, 10:50 AM
I am still trying to figure out what to do with that NSMatrix stuff.

I have made a test project. The project's nib looks like the one I am attaching below. I have not written any extra code, I have only written this:
int i;
NSArray *arr = [mymatrix cells];

int originalSize = [arr count];
int columns = originalSize / 2;

NSArray *arr1 = [arr subarrayWithRange:NSMakeRange(0,columns+1)];
if (!arr) {

[mymatrix removeColumn:0];
[mymatrix addColumnWithCells:arr1];
[mymatrix sizeToCells];

And it gives me a runtime error. Why? I am only trying to resize the matrix. Now it displays a column with 10 rows. I want it to display two columns with 5 elements. How can I do that?

Jan 20, 2007, 03:37 PM
The attached works perfectly for me. I think that removing the only column is unwise...