Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 23, 2009, 06:09 PM   #1
medasmx
macrumors member
 
Join Date: Nov 2008
tableview

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
medasmx is offline   0 Reply With Quote
Old Jun 23, 2009, 06:11 PM   #2
medasmx
Thread Starter
macrumors member
 
Join Date: Nov 2008
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
medasmx is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: NumberFormatter in TableView Qaanol Mac Programming 3 Dec 25, 2013 07:56 PM
parsing and displaying in tableview jennylcf iPhone/iPad Programming 2 Jan 28, 2013 06:31 AM
Populating TableView Mvkoe iPhone/iPad Programming 5 Dec 1, 2012 04:34 PM
TableView is Empty RagingGoat iPhone/iPad Programming 74 Nov 5, 2012 01:08 PM
Resolved: Help with TableView data troop231 iPhone/iPad Programming 2 Jun 26, 2012 12:49 PM

Forum Jump

All times are GMT -5. The time now is 05:15 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC