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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Use TableView without TableView Delegate or DataSource mikezang iPhone/iPad Programming 3 Dec 15, 2010 11:58 AM
Please help my understand the tableview thing HawaiiMacAddict Mac Programming 4 Jun 8, 2008 11:57 PM
iphone sdk beta 3 - tableview cmaier Mac Programming 0 Apr 8, 2008 09:30 PM
Array+ TableView Help CaptainZap Mac Programming 2 Jan 9, 2008 03:46 PM
Cocoa TableView Binding w/ NSMutableArray of NSNetService - Assistance Requested justindz Mac Programming 2 Jan 16, 2006 09:13 AM


All times are GMT -5. The time now is 09:30 PM.

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

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