Cocoa: Programmmatic creation of an NSMatrix.

Discussion in 'Mac Programming' started by Soulstorm, Jan 18, 2007.

  1. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #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?
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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...
     
  3. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #3
    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?
     

    Attached Files:

  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    The attached works perfectly for me. I think that removing the only column is unwise...
     

    Attached Files:

Share This Page