PDA

View Full Version : drawAtPoint




medasmx
Jun 12, 2009, 05:37 PM
This is a follow-up to a prior post, where I was trying to come up with custom cells in a table view. Kainjow gave some suggestions, which helped a lot, and I was able to draw a tableview with custom square cells. Now I am trying to put text in these cells. The below code compiles without error but the text does not appear.


Here is the code for the custom cell...

#import "MyCell.h"

@implementation MyCell

-(void)prepareAttributes
{
attributes=[[NSMutableDictionary alloc]init];
[attributes setObject:[NSFont fontWithName:@"Helvetica" size:20] forKey:NSFontAttributeName];
[attributes setObject:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
}

- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (!self) return nil;
[self prepareAttributes];
string=@"jimmy";
return self;
}

-(void)setString:(NSString*)c
{
c=[c copy];
[string release];
string=c;
[self setNeedsDisplay:YES];
}

-(void)drawStringCenteredIn:(NSRect)rect
{
[string drawAtPoint:NSZeroPoint withAttributes:attributes];
}

-(BOOL)isFlipped
{
return YES;
}

-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView
{
[[NSColor redColor]set];
NSRectFill(NSInsetRect(cellFrame,4,4));
[self drawStringCenteredIn:cellFrame];
}

@end



Next the code for the tableview...

#import "AppController.h"
#import "MyCell.h"

@implementation AppController

-(void)awakeFromNib
{
data=[[NSArray alloc]initWithObjects:@"1",@"2",@"3",nil];
myCell=[[[MyCell alloc]init]autorelease];
NSTableColumn*column=[tv tableColumnWithIdentifier:@"columnOne"];
[column setDataCell:myCell];
NSTableColumn*column2=[tv tableColumnWithIdentifier:@"columnTwo"];
[column2 setDataCell:myCell];
NSTableColumn*column3=[tv tableColumnWithIdentifier:@"columnThree"];
[column3 setDataCell:myCell];
[tv setRowHeight:55];
}

-(int)numberOfRowsInTableView:(NSTableView *)tv
{
return [data count];
}

-(id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row
{
return [data objectAtIndex:row];
}

@end


Again, thanks for any suggestions. Adam



kainjow
Jun 12, 2009, 07:48 PM
Cells must draw within their cellFrame, which is relative to the cell's control bounds. So if you're using NSZeroPoint then this is {0,0} within the view's bounds, and may be overwritten by something else in the view. Instead you should use values relative to cellFrame, so in your case try something like NSMakePoint(NSMinX(rect), NSMinY(rect)) - but usually in tables setFlipped is on, so you may need to change the Y value.

medasmx
Jun 13, 2009, 07:50 PM
Below is some code that uses drawInRect, that I found in a customtableview that I downloaded.


#import "MyCell.h"

@implementation MyCell

-(void)prepareAttributes
{
attributes=[[NSMutableDictionary alloc]init];
[attributes setObject:[NSFont fontWithName:@"Helvetica" size:20] forKey:NSFontAttributeName];
[attributes setObject:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
}

- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (!self) return nil;
[self prepareAttributes];
string=@"jimmy";
return self;
}


-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView
{
NSRect anInsetRect = NSInsetRect(cellFrame,10,10);
NSRect aTextBox = NSMakeRect(anInsetRect.origin.x,anInsetRect.origin.x,25,25);
[[NSColor redColor]set];
NSRectFill(NSInsetRect(cellFrame,4,4));
[string drawInRect:aTextBox withAttributes:attributes];
}

@end


I still don't get any text in the custom cells in the table view. Any ideas? Thanks. Adam

medasmx
Jun 14, 2009, 09:21 PM
A book that I have been reading recently is the one by Anguish, et.al., which I would recommend. It is well written, though dated a few years. In chapter 14, they go through an example of putting text in a custom view. In part of this they use the [self bounds] expression. I get an error when trying to make use of this.

Below is the code (only the first two lines come from their book)...


-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView
{
NSRect myRect=NSZeroRect;
NSRect myBounds=[self bounds];
NSRect anInsetRect = NSInsetRect(cellFrame,10,10);
NSRect aTextBox = NSMakeRect(anInsetRect.origin.x,anInsetRect.origin.x,25,25);
[[NSColor redColor]set];
NSRectFill(NSInsetRect(cellFrame,4,4));
[string drawInRect:aTextBox withAttributes:attributes];
}


"invalid initializer" is the error message. In the example they use myRect and myBounds to define a point where drawAtPoint can be used to place the text in the custom view. Why am I getting this error message? Thanks.

Adam