tableview

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

  1. macrumors member

    Joined:
    Nov 9, 2008
    #1
    I have been working on custom cells for a table view. Kainjow gave some suggestions, and the below code does a 3x3 tableview, gray boxes, with some text in the upper left corner. Appreciate the help.

    First, 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)frame {
        self = [super initWithFrame:frame];
        if (!self) return nil;
    	[self prepareAttributes];
        return self;
    }
    
    -(void)setString:(NSString*)c
    {
    	c=[c copy];
    	[string release];
    	string=c;
    	[self setNeedsDisplay:YES];
    }
    
    -(void)drawStringCenteredIn:(NSRect)cellFrame
    {
    	NSPoint textPoint;
    	
    	NSString*string=@"jimmy";
    	
    	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
    
    Next, the appcontroller, with tableview...
    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];
    }
    
    @end
    
    What I want to do next is be selective as to where the text goes. I added a popupbutton and tried to start by selectively adding the text to columns individually. Below is the new code for 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)myAction:(id)sender;
    {
    	NSString*buttonString=[(NSPopUpButton*)asmbutton1 titleOfSelectedItem];
    	NSString*mySecondString=@"jimmy";
    	mySecondCell=[[[MyCell alloc]init]autorelease];
    	[mySecondCell setString:mySecondString];
    	
    	NSTableColumn*column=[tv tableColumnWithIdentifier:@"columnOne"];
    	NSTableColumn*column2=[tv tableColumnWithIdentifier:@"columnTwo"];
    	NSTableColumn*column3=[tv tableColumnWithIdentifier:@"columnThree"];
    	
    	if ([buttonString isEqualTo:@"1"])
    		[column setDataCell:mySecondCell];
    	else if ([buttonString isEqualTo:@"2"])
    		[column2 setDataCell:mySecondCell];
    	else [column3 setDataCell:mySecondCell];	
    }
    
    @end
    
    In the original custom cell, the text is set to "". "jimmy" should be added to columns when the popupbutton is changed. Any ideas? Thanks.

    Adam
     
  2. thread starter macrumors member

    Joined:
    Nov 9, 2008
    #2
    addendum

    Sorry, forgot to mention that the first set of code works fine. When trying to add the popupbutton, it runs, but the text isn't added. Again, thanks.

    Adam
     

Share This Page