PDA

View Full Version : custom cells tableview, part2




medasmx
Jul 3, 2009, 03:48 PM
This is the newest version of my custom cell tableview (last post titled "custom cells in tableview") program. You can refer to the earlier post for a version that would print text in each column. What I want to do is put text in individual cells. The code below runs without errors, but when the popupbutton is changed from "1" (cell 0,0) to "4" (cell 1,1), nothing happens. In my earlier version, the text appeared appropriately, but not in individual cells, only in each column.

custom cell code ...

@implementation MyCell

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

- (id)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];

if (!self) return nil;
[self prepareAttributes];
string=@"Jimmy";
return self;
}

-(void)dealloc
{
[string release];
[super dealloc];
}

-(NSString*)string
{
return string;
}

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

-(void)drawStringCenteredIn:(NSRect)cellFrame
{
NSPoint textPoint;

textPoint.x = cellFrame.origin.x + 5;
textPoint.y = cellFrame.origin.y;

[string drawAtPoint:textPoint withAttributes:attributes];
}


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

[self drawStringCenteredIn:cellFrame];
}

@end


app controller code...


@implementation AppController

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

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

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

-(id)dataCellForRow:(int)row
{
someCell=[[[MyCell alloc]init]autorelease];
someCell=[super dataCellForRow:row];

NSString*mySecondString=@"jimmy";
NSString*myThirdString=@"";
mySecondCell=[[[MyCell alloc]init]autorelease];
myThirdCell=[[[MyCell alloc]init]autorelease];
[mySecondCell setString:mySecondString];
[myThirdCell setString:myThirdString];

NSTableColumn*column=[tv tableColumnWithIdentifier:@"columnOne"];
NSTableColumn*column2=[tv tableColumnWithIdentifier:@"columnTwo"];

if ([(NSPopUpButton*)asmbutton1 titleOfSelectedItem]==1){
[column setDataCell:mySecondCell];
[column2 setDataCell:myThirdCell];
if (row==2) [someCell setObjectValue:myThirdCell];
}

if ([(NSPopUpButton*)asmbutton1 titleOfSelectedItem]==2){
[column setDataCell:myThirdCell];
[column2 setDataCell:mySecondCell];
if (row==2) [someCell setObjectValue:myThirdCell];
}

if ([(NSPopUpButton*)asmbutton1 titleOfSelectedItem]==3){
[column setDataCell:mySecondCell];
[column2 setDataCell:myThirdCell];
if (row==1) [someCell setObjectValue:myThirdCell];
}

if ([(NSPopUpButton*)asmbutton1 titleOfSelectedItem]==4){
[column setDataCell:myThirdCell];
[column2 setDataCell:mySecondCell];
if (row==1) [someCell setObjectValue:myThirdCell];
}

return someCell;
}

@end


Any help is appreciated. Adam