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

medasmx

macrumors member
Original poster
Nov 9, 2008
89
0
I have been struggling with creating custom cells to go in a matrix. I put up a prior thread, titled nsmatrix, that did a matrix of nstextfieldcells. I got it to work. Now I want to do the same thing, but replace the textfieldcells with custom cells. There is some sample code online. Everyone seems to create a nsimage, and use it to draw the custom cells. Below is the code I have tried for the custom cell.

Code:
#import "myCell.h"

@implementation myCell

-(id)init
{
if(![super init]) return nil;
return self;
}

- (void)drawInteriorWithFrame:(NSRect)theCellFrame inView:(NSView *)theControlView
{
	NSRect anInsetRect = NSInsetRect(theCellFrame,5,5);
	NSImage*myImage=[[NSImage alloc]initWithSize:NSMakeSize(150,150)];
	[myImage lockFocus];
	NSRect myRect=NSMakeRect(0,0,100,100);
	[myImage unlockFocus];
	
	NSRect myBox = NSMakeRect(anInsetRect.origin.x,
									  anInsetRect.origin.y,
									 anInsetRect.size.width*0.5,
									  anInsetRect.size.height*0.5);

	[myImage drawInRect:myBox fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
}

@end

I suspect the problem is here, where I doubt I have drawn the nsimage properly. All I want is a standard rectangle.

Code:
	[myImage lockFocus];
	NSRect myRect=NSMakeRect(0,0,100,100);
	[myImage unlockFocus];

The code for the matrix part is similar to my prior post. I would appreciate any input. Thanks. Adam
 
Your code is creating an image but not drawing anything into the image, and then it's drawing the image. So you're really just drawing a blank image. But since you're not loading an image, it's unnecessary to create one in the first place.

Here is an example of something basic:

Code:
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    NSRect rect = NSInsetRect(cellFrame, 5.0, 5.0);
    [[NSColor redColor] set];
    NSRectFill(rect);
}
 
Thanks, kainjow, for your input. I used your code to create the custom cell, but it still doesn't work. In the debugger, it says " Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (ASKNibConnector)". I guess there is a problem in the appcontroller I made, loading the custom cells into the matrix.

I included the code for my appcontroller below. The exact same code worked for my other matrix program (which I posted), there using nstextfieldcells.

Code:
#import "AppController.h"
#import "myCell.h"

@implementation AppController

-(void)awakeFromNib
{
aCell=[[myCell alloc]init];
[myMatrix setPrototype:aCell];
[myMatrix setTarget:self];
[myMatrix setAction:@selector(myPopUp:)];
[self myPopUp:nil];
}

-(IBAction)myPopUp:(id)sender
{
aCell=[[myCell alloc]init];
[myMatrix setPrototype:aCell];
[myMatrix setMode:NSRadioModeMatrix];
adamString=[(NSPopUpButton*)sender titleOfSelectedItem];
if([adamString isEqualToString:@"one"])
	[myMatrix putCell:aCell atRow:0 column:0];
if([adamString isEqualToString:@"two"])
	[myMatrix putCell:aCell atRow:0 column:1];
if([adamString isEqualToString:@"three"])
	[myMatrix putCell:aCell atRow:1 column:0];
if([adamString isEqualToString:@"four"])
	[myMatrix putCell:aCell atRow:1 column:1];
}

@end

Again, any help is appreciated. Adam
 
Looks like there might be some problem with the nib. Hard to tell from that code though.

Here's a sample project on some basic usage of NSMatrix. It's almost easier to create them from code than it is in the nib (which AFAIK doesn't have a blank NSMatrix control you can use, unless I missed it). Have a look at it and maybe it'll help you fix the issues in your code.
 

Attachments

  • MatrixCells.zip
    59.2 KB · Views: 130
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.