How do i refresh the matrix with new values like the tableview's reloadData

Discussion in 'Mac Programming' started by zophtx, May 13, 2010.

  1. zophtx macrumors member

    Joined:
    Mar 8, 2010
    Location:
    Inside a Cocoa Book
    #1
    (question in last post) :)



    Im try to assign a value to a cell in a matrix column and row to a specify
    value; for example i would like the matrix to look like this:
    -------------
    |1|2|3|4|5|
    --------------
    |6|7|8|9|10|
    --------------

    i know i could do this wit this code:
    Code:
    int i;
    
    for(i=1; i<=10; i++){
            cell = [matrix cellAtRow:0 column:i-1];
            [cell setTitle:[NSString stringWithFormat:@"%d",i]];
    }
    
    the reason i dont want to do it this way is because each column is going to be assign a diffent value when a value eqaul a defferent value;

    and i know i could do this by manually do it in IB by create a matrix there.
    but i'm doing it manually by code.

    i tryed this code:
    Code:
    	
    int c,r,i;
    i=1;
    while (i<=10) {
    	for(c=0; c<=5; c++){
    		cell = [matrix cellAtRow:0 column:c];
    		[cell setTitle:[NSString stringWithFormat:@"%d",c];
    	}
    	for(r=0; r<=2; r++){
    		cell = [matrix cellAtRow:r column:0];
    		[cell setTitle:[NSString stringWithFormat:@"%d",r]];
    	}
    	i++;
    }
    
    this doesnt work
    output
    |0|1|2|3|4|
    -------------
    |1|

    soo may you point me in the right direction?
    thank you :)
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Point to the place in your code where the counter i is being used to assign a value to a matrix cell.

    Also, you should learn how to mentally step through your code and confirm it's doing what it should. Your loops are wrong for what you described as your desired result. If you stepped through them, either mentally, with pencil and paper, or using a debugger, the problems would become obvious.

    The posted code looks like you don't have a clear design, and you're trying to write code and hoping for a design. That almost never works. You should sit down and carefully design your algorithm in plain language, and walk through it mentally to make sure it will work. Only then should you start coding.
     
  3. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #3
    You should probably also learn about nested loops. That might get you going in the right direction.
     
  4. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #4
    Does it seem very odd to everyone else that he's using a while loop 100% iteratively instead of conditionally?
     
  5. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #5
    Mostly it seems that he has a ways to go to grasp programming basics. Perhaps he needs a better instructor or a better thought out course.
     
  6. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #6
    Ya just trying to nudge him in the right way.

    Anywho... OP maybe you should think about putting your columns loop INSIDE the rows loop or vice versa.
     
  7. zophtx thread starter macrumors member

    Joined:
    Mar 8, 2010
    Location:
    Inside a Cocoa Book
    #7
    ya i tried that. but had trouble along the road but i figured it out here is the code snippet (cell and matrix are the the actually variables names - they uses as example vars);

    Code:
    {	
            int row, column;
    	int i=1;
    	
    	
    	for (row=0; row<2; row++)
    		for (column=0; column<5; column++){
    			cell = [matrix cellAtRow:row column:column];
    			[cell setTitle:[NSString stringWithFormat:@"%i",i++]];
    			
    		}
    	
    }
    
    thanks for helpful comments :)
     
  8. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #8
    Personally I'd get rid of the "i" variable and think of a way of representing the value as a combination of the variables you already have. Such as...

    Code:
    {	
            int row, column;
    	#define COLUMNS 5
    	#define ROWS 2	
    
    	
    	for (row=0; row<ROWS; row++)
    		for (column=0; column<COLUMNS; column++){
    			cell = [matrix cellAtRow:row column:column];
    			[cell setTitle:[NSString stringWithFormat:@"%d", (row*COLUMNS) + column + 1]];
    			
    		}
    	
    }
    
     
  9. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    I'm not sure that's clearer. The postincrement is a bit questionable in zophtx's last example, but I think doing that sort of math doesn't really clarify things. I guess to toss another option into the mix:

    Code:
    for(int x =0 ; x <ROWS*COLUMNS; x++) {
      row = x/COLUMNS;
      column=x%COLUMNS;
      cell = [matrix cellAtRow:row column:column];
      [cell setTitle:[NSString stringWithFormat:@"%d",x+1];
    }
    But really, zophtx's last example seemed clear enough to me (though using constants rather than hardcoding is definitely a better idea).

    -Lee
     
  10. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #10
    Its an odd example simply because every cell increments up, the i variable is clear enough in this example and I didn't want to TOTALLY rewrite the OP's code. USUALLY when working on a problem like this you are representing some kind of "data" where the individual cell number is dependent on some kind of mathematical model. Using the #defines allows you to change the size of your box easier, but one could use variables for them as well.

    For example a multiplication table.
    [cell setTitle:[NSString stringWithFormat:mad:"%d",(row*column)]];
    or
    [cell setTitle:[NSString stringWithFormat:mad:"%d",((row+1)*(column+1))]];

    Also, your code has an error in it ;) I get bonus points for that right!?
     
  11. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #11
    Ah, balls. I was copying and pasting and missed the closing ]. Oh well. I wouldn't expect the OP to decide that my suggestion was better and try to copy and paste. I was really just piling on.

    -Lee
     
  12. zophtx thread starter macrumors member

    Joined:
    Mar 8, 2010
    Location:
    Inside a Cocoa Book
    #12
    this method doesnt need any #define macros but this is very interesting.
    thanks again for the support.
     
  13. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #13
    What do you mean "this method", you don't need the COLUMNS or ROWS macros in that particular example. The idea of the #define s are to make it easier to change the size or scope of your application by just changing the number in one place. Presumably in the future you might need to do something like divide the screen up into even COLUMNS or draw COLUMNS*ROWS boxes around your numbers or something.

    Everywhere you see a comparison to a static number like 5, you could probably replace it with a #define to make it easier to read and change later.
     
  14. zophtx thread starter macrumors member

    Joined:
    Mar 8, 2010
    Location:
    Inside a Cocoa Book
    #14
    sorry i meant "this statement" my terminology is a bit off yesterday.:p

    but ya i know what yo mean about using #define to simplize the task of resize the size of matrix instead of going and looking where the code is the sets the size of the matrix.
     
  15. zophtx thread starter macrumors member

    Joined:
    Mar 8, 2010
    Location:
    Inside a Cocoa Book
    #15
    How do i refresh the matrix with new values like the tableview's reloadData

    is there any thing like the tableview's -reloadData method for the matrix?
    im posting in the thread cause i dont want to make a another thread snice basiclly this does have to do with "NSMatrix Alignment" so how can i update the values of the nsmatrix.

    is there a -reloadData method for nsmatrix or something like it?
     
  16. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #16
    A tableview does not contain its data, whereas a matrix does. It is a subclass of NSView, so I would think -display would do the trick
     

Share This Page