Custom NSCells

Discussion in 'Mac Programming' started by Soulstorm, Feb 24, 2010.

  1. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #1
    I am having a hard time setting up custom NSCells.

    How can I set up a custom NSCell that displays parameters from an array that contains object with more than one property?

    I have already managed to make a custom NSCell by overriding the "drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView" method.

    Code:
    - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
    {
    	NSString *firstNameString = [self objectValue];
     
    	NSPoint textPoint;
     
    	textPoint.x = cellFrame.origin.x + 1;
    	textPoint.y = cellFrame.origin.y;
     
    	NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSColor blackColor], NSForegroundColorAttributeName, [NSFont systemFontOfSize:13], NSFontAttributeName, nil];
    	[firstNameString drawAtPoint:textPoint withAttributes:textAttributes];
    }

    But how do I manage to display information with multiple values in a Cell?
     
  2. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #2
    You can draw as much as you want in the cell. Use as many steps as you need within the method. I cannot remember or determine whether the frame clips the drawing, I am thinking not, other than clipping by the view itself. If you need to insure that the frame is large enough to fit the contents, you will have to do that from outside this method (e.g., if it is a table view, you would have to use the delegate to adjust the row/column boundaries to fit the needed content). You should probably avoid making adjustments to the interior frame from within this method.
     
  3. Soulstorm thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #3
    I believe you didn't fully understand my question, which may be my own fault, coming to think of it.

    My question was not how I could draw many things into the cells, but how can I associate those elements with the things in the Cell.

    Specifically, this is my code:
    for the tableView:

    Code:
    - (void)awakeFromNib
    {
    	self.items = [NSMutableArray array];
    	[self.items addObject:[[TestClass alloc]initWithProperty1:@"asdsa" and2:@"eeee" and3:@"dsf4423"]];
    	//[self.items addObject:@"hellow!"];
    	
    	CustomCell *cell = [[CustomCell alloc]init];
    	[self.tableColumn setDataCell:cell];
    }
    
    - (NSInteger) numberOfRowsInTableView:(NSTableView *)tableView
    {
    	return [self.items count];
    }
    
    
    - (id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
    {
    	return [self.items objectAtIndex:row];
    }
    
    - (void) dealloc
    {
    	[tableView release];
    	[tableColumn release];
    	[items release];
    	[super dealloc];
    }
    
    @end
    my custom test class:

    Code:
    @synthesize property1, property2, property3;
    
    - (id) init
    {
    	self = [super init];
    	if (self != nil) {
    		self.property1 = [NSString string];
    		self.property2 = [NSString string];
    		self.property3 = [NSString string];
    	}
    	return self;
    }
    
    - (id)copyWithZone:(NSZone *)zone
    {
    	TestClass *copy = [[[self class]allocWithZone:zone]init];
    	return copy;
    }
    
    + (TestClass *)testClassWithProperty1:(NSString *)prop1 and2:(NSString *)prop2 and3:(NSString *)prop3
    {
    	return [[[self alloc]initWithProperty1:prop1 and2:prop2 and3:prop3]autorelease];
    }
    
    + (NSArray *)testClassArray
    {
    	static NSArray *testClassKeys = nil;
    	if (testClassKeys == nil) {
    		testClassKeys = [[NSArray alloc]initWithObjects:@"property1", @"property2", @"property3", nil];
    	}
    	return testClassKeys;
    }
    
    - (NSDictionary *)testClassDictionary
    {
    	return [self dictionaryWithValuesForKeys:[[self class]testClassArray]];
    }
    
    - (id)initWithProperty1:(NSString *)prop1 and2:(NSString *)prop2 and3:(NSString *)prop3
    {
    	self = [self init];
    	self.property1 = prop1;
    	self.property2 = prop2;
    	self.property3 = prop3;
    	return self;
    }
    
    + (NSSet *)keyPathsForValuesAffectingPersonDictionary
    {
    	return [NSSet setWithObjects:@"property1", @"property2", @"property3", nil];
    }
    
    - (void) dealloc
    {
    	[property1 release];
    	[property2 release];
    	[property3 release];
    	[super dealloc];
    }
    
    my custom cell:
    Code:
    - (id) copyWithZone:(NSZone *)zone
    {
    	CustomCell *cell = (CustomCell *)[super copyWithZone:zone];
    	return cell;
    }
    
    - (void) drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
    {
    	NSDictionary *cellValues = [self objectValue];
    	
    	NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSColor blackColor], NSForegroundColorAttributeName, [NSFont systemFontOfSize:13], NSFontAttributeName, nil];
    	NSPoint textPoint;
    	
    	NSString *helloString = [cellValues valueForKey:@"property2"];
    	
    	textPoint.x = cellFrame.origin.x + 1;
    	textPoint.y = cellFrame.origin.y;
    	[helloString drawAtPoint:textPoint withAttributes:textAttributes];
    }
    
    
    - (void) dealloc
    {
    	[titleTextFieldCell release];
    	[subtitleTextFieldCell release];
    	[super dealloc];
    }
    
    All this code gives me is the attached output. I wanted to give the value of "property1" but all I get is the name of the address of the value!


    Can anyone help me?
     

    Attached Files:

  4. Soulstorm thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #5
    I did, and I managed to make something, but I am stuck. It seems that my custom table view is representing only the first item's properties, and nothing else. I am attaching the entire project, in case someone needs to take a look.
     

    Attached Files:

  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    The wretched copy-paste errors ;)

    You create class1 and class2, but instead of assigning values to class2 you assign the same things to class1 again.
     

Share This Page