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

zophtx

macrumors member
Original poster
Mar 8, 2010
45
0
Inside a Cocoa Book
(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 :)
 
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.
 
You should probably also learn about nested loops. That might get you going in the right direction.
 
Does it seem very odd to everyone else that he's using a while loop 100% iteratively instead of conditionally?
 
Does it seem very odd to everyone else that he's using a while loop 100% iteratively instead of conditionally?

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.
 
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.

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.
 
think about putting your columns loop INSIDE the rows loop or vice versa.

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 :)
 
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);
...

thanks for helpful comments :)
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]];
			
		}
	
}
 
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]];
			
		}
	
}

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
 
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

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!?
 
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!?

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
 
For example a multiplication table.
[cell setTitle:[NSString stringWithFormat:mad:"%d",(row*column)]];
or
[cell setTitle:[NSString stringWithFormat:mad:"%d",((row+1)*(column+1))]];

this method doesnt need any #define macros but this is very interesting.
thanks again for the support.
 
this method doesnt need any #define macros but this is very interesting.
thanks again for the support.

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.
 
What do you mean "this method"
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.
 
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?
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.