custom cells in tableview

Discussion in 'Mac Programming' started by medasmx, Jun 30, 2009.

  1. macrumors member

    Joined:
    Nov 9, 2008
    #1
    I have been working on this custom cells for table view for a while, through various posts. The code below works and does the following...creates a 3x3 table view of gray cells. There is a pop-up button that will print text ("jimmy" here), depending on which column is selected (1 through 3). Appreciate prior input of Kainjow.

    Here is the code for the custom cells...

    Code:
    #import "MyCell.h"
    
    @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
    
    Here is the code for the appcontroller...

    Code:
    #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:100];
    }
    
    -(int)numberOfRowsInTableView:(NSTableView *)tv
    {
    	return [data count];
    }
    
    -(id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row 
    {
    	return [data objectAtIndex:row];
    }
    
    -(IBAction)asmbutton1:(id)sender;
    {
    	NSString*buttonString=[(NSPopUpButton*)sender titleOfSelectedItem];
    	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"];
    	NSTableColumn*column3=[tv tableColumnWithIdentifier:@"columnThree"];
    	
    	if ([buttonString isEqualToString:@"1"]) {
    	[column setDataCell:mySecondCell];	
    	[column2 setDataCell:myThirdCell];
    	[column3 setDataCell:myThirdCell];}
    	
    	else if ([buttonString isEqualToString:@"2"]) {
    	[column2 setDataCell:mySecondCell];
    	[column setDataCell:myThirdCell];
    	[column3 setDataCell:myThirdCell];}
    	
    	else {[column3 setDataCell:mySecondCell];	
    	[column setDataCell:myThirdCell];
    	[column2 setDataCell:myThirdCell];}
    }
    
    @end
    
    What I really want to do is to have a pop-up 1-9 and be able to put the text in one specific cell, not just the whole column. I read some about dataCellForRow:, but haven't figured it out yet. Any help is appreciated. Thanks

    Adam
     

Share This Page