Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
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?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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...
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
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:
Code:
-(void)awakeFromNib
{
	int i;
	NSArray *arr = [mymatrix cells];
	
	int originalSize = [arr count];
	int columns = originalSize / 2;
	
	NSArray *arr1 = [arr subarrayWithRange:NSMakeRange(0,columns+1)];
	if (!arr) {
		NSLog(@"!!!");
	}
	
	[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?
 

Attachments

  • Picture 1.png
    Picture 1.png
    29.1 KB · Views: 120

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
The attached works perfectly for me. I think that removing the only column is unwise...
 

Attachments

  • MatrixText.zip
    35.9 KB · Views: 394
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.