Re: NSMatrix help
Re: NSMatrix help
- Subject: Re: NSMatrix help
- From: Fritz Anderson <email@hidden>
- Date: Fri, 8 Oct 2004 09:24:13 -0500
In my test project, the following worked:
- (void) awakeFromNib
{
[matrix addColumn];
}
- (IBAction) addText: (id) sender
{
NSString * theString = [textField stringValue];
NSCell * newCell = [[NSCell alloc] initTextCell: theString];
[matrix addRowWithCells: [NSArray arrayWithObject: newCell]];
[newCell release];
}
The
-addColumn message in
-awakeFromNib silenced the assertion by ensuring the
NSMatrix had enough columns to accept the row.
The documentation for
-[NSMatrix addRowWithCells:] says that an empty
NSMatrix should resize itself to as many columns as there are elements in the
NSArray passed as the argument. Apparently this is not so.
Some memory management notes: I use a convenience class method of
NSArray, thus getting an autoreleased array to pass to
addRowWithCells:. If I used
alloc/
init, as you did, I'd have to release it myself. I do have to release
newCell when I'm done with it.
Also,
NSLog([[NSNumber numberWithInt:[myMatrix numberOfRows]] stringValue]);
may happen to work, but is just wrong. The first parameter to
NSLog() is a format string, and thus cannot be any arbitrary
NSString. Some day, a % character is going to appear in that first string, and NSLog() will look for a stack parameter that isn't there, crashing your program. What you meant was:
NSLog(@"Row count = %d", [myMatrix numberOfRows]);
-- F
On 5 Oct 2004, at 9:55 PM, James Andrews wrote:
I created a new project. In IB I added a button, a text field, and a
custom view. Changed the class on the view to NSMatrix. Created my
Controller, with an action and 2 outlets, one pointing to the view the
other to the text input field. And make the button access my single
action. This is the code for my action.
- (IBAction)addCell:(id)sender
{
NSCell * myText = [[NSCell alloc] initTextCell:[inputText objectValue]];
NSMutableArray * cellArray = [[NSMutableArray alloc] init];
[cellArray insertObject:myText atIndex:[cellArray count]];
if([myMatrix numberOfRows] == 0){
[myMatrix addRowWithCells:cellArray];
}
NSLog([[NSNumber numberWithInt:[myMatrix numberOfRows]] stringValue]);
NSLog([[NSNumber numberWithInt:[myMatrix numberOfColumns]] stringValue]);
}
...
2004-10-05 22:54:18.720 TestDraw[3296] *** Assertion failure in
-[NSMatrix insertRow:withCells:], AppKit.subproj/NSMatrix.m:1213
2004-10-05 22:54:18.727 TestDraw[3296] Invalid parameter not
satisfying: !newCells || ([newCells count] == _numCols)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden