Re: Resize Matrix
Re: Resize Matrix
- Subject: Re: Resize Matrix
- From: "Louis C. Sacha" <email@hidden>
- Date: Sat, 8 May 2004 14:39:23 -0700
Hello...
First, make sure you are sending a setNeedsDisplay:TRUE message after
you finish changing the layout of your matrix using
renewRows:columns:. If you don't, some cells may redraw but others
will not, which can cause strange results.
If that's not the problem, you might need to change the way you
update the matrix after renewing it. Because of the way the
renewRows:columns: method caches extra cells, it doesn't really keep
any order among the displayed cells when the size changes.
It would probably be easiest to seperate your data model (the content
that is displayed in the cells) from the matrix. Then, whenever the
matrix is resized, you would just reset the contents of all the cells
so that they reflect the model correctly.
For example, if your matrix was displaying a simple list of strings,
the data model could be an NSArray of those strings.
NSArray *model = [NSArray arrayWithObjects:@"A",@"B ",@"C
",@"D ",@"E ",@"F ",nil];
Then when you wanted to resize the matrix, you could call a method in
your controller class that looks something like this:
- (void)resizeMatrix:(NSMatrix *)matrix withContent:(NSArray
*)contents toRows:(int)rows columns:(int)columns
{
[matrix renewRows:rows columns:columns];
unsigned numberOfItems = [contents count];
unsigned index = 0;
NSString *cellString = nil;
int r, c;
for (r = 0; r < rows; r++)
{
for (c = 0; c < columns; c++)
{
if (index < numberOfItems) {cellString =
[contents objectAtIndex:index];}
else {cellString = @"";}
[[matrix cellAtRow:r column:c]
setStringValue:cellString];
index ++;
}
}
[matrix setNeedsDisplay:TRUE];
}
If you are using a mixture of different types of cells, or the
contents of the cells are more complex, you could keep an NSArray
containing the cells, and then use the NSMatrix instance method
putCell:atRow:column: to do something similar to the above code.
Instead of setting the string, you would be replacing the entire cell.
It also might be possible to use the NSMatrix
sortUsingFunction:context: or sortUsingSelector: instance methods to
sort the cells into the correct order, but it might not work
correctly if you are using renewRows:columns: because of the cached
extra cells.
Hope that helps,
Louis
Hi,
The "renewRows: columns:" method is for resizing the matrix, but this method
doesn't change the position of the cells accordingly. For example the cell
at (0, 3) position in a (4 * 5) matrix should move to (1, 1) position when
the matrix is resized to (10 * 2).
"renewRows:columns:" creates new rows or columns but doesn't reposition the
cells.
Alternatively, I collected all the cells of the matrix to an NSArray before
resize and used that to reconstructed the matrix after resize. In this
process I used "addRowWithCells" method. But here also the cells that are
newly created appear blank and the position of the cell is not getting
changed.
Is anyone there facing the same problem?, Please give your suggestions to
overcome this problem.
Also, did anyone tried to remove a single cell from a matrix and reconstruct
the matrix?
Thanks in advance,
Regards,
Nagarajan
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.