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
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
