Some favorite site feeds aggregated locally: iPhone Development RSS   Adobe Labs RSS   Macrumors RSS

Wednesday, May 6, 2009

Getting columned data in a UITableView

Wednesday, May 6, 2009    0 Comments

Since I'm still fairly green to this whole Objective-C thing and I am still learning the APIs that are available (there are a billion of them), I don't always know how to do exactly what I want to do. A case in point... I wanted to present a high scores screen in a little game I am coding up (a learning exercise... I highly recommend coding a game to expose yourself to tons of little challenges).

I have an NSMutableArray that I have stuffed with NSDictionary items. Each of those has a key for name and score. Easy. However I thought about using the "\t" character like we can do in ActionScript 3 and implement a tabStop somehow... thus negating the need to use dictionaries at all and just munge the name with the score.

Obviously this makes sorting an extremely stupid activity (many hoops to needlessly jump through). So it's not a solution by any means (an array of dictionaries is), but I wanted to see how I might replicate the functionality of tabStops in Objective-C (non-paragraph).

I have yet to find the solution. However in beating myself up in trying to figure out how to properly implement NSTextTab for a non UITextField I came across a true solution. Adding subviews!

This is exactly what I was after, and it has the benefit of allowing tons of flexibility in how to best present your textual data in the UITableView.

Without dressing the main method up too much, I think you'll be able to see what's going on here. I am only using a straight NSArray for this example, but you can see how this works.
// Set up the table view cells
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

UILabel *label; // for scores
UILabel *nameLabel;

if( cell == nil ){

CGRect frame = CGRectMake(0,0,300,44);
cell = [[[UITableViewCell alloc] initWithFrame:frame reuseIdentifier:SimpleTableIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;

//Create the score label in the cell
label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 10.0, 70.0, 25.0)] autorelease];
label.tag = 1;
label.font = [UIFont boldSystemFontOfSize:18.0];//systemFontOfSize
label.textAlignment = UITextAlignmentRight;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
label.text = @"1000";

// Create the name label in the cell
nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(80.0, 10.0, 195.0, 25.0)] autorelease];
nameLabel.tag = 2;
nameLabel.font = [UIFont boldSystemFontOfSize:18.0];
nameLabel.textColor = [UIColor whiteColor];
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.lineBreakMode = UILineBreakModeWordWrap;
nameLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:nameLabel];

} else {
// Out of view items
label = (UILabel *)[cell.contentView viewWithTag:1];
nameLabel = (UILabel *)[cell.contentView viewWithTag:2];
}

NSInteger row = [indexPath row];
nameLabel.text = [listData objectAtIndex:row];
cell.selected = NO;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

Labels: , , , ,

 

Friday, December 5, 2008

Free Cocoa framework for Flash implementation

Friday, December 5, 2008    0 Comments

Flash'In'App is a free Cocoa framework that lets you load and fully manage Adobe® Flash movies directly from your own applications for Mac OS X. Flash'In'App contains a set of classes, which provide any application with the abilities to play SWF files, manage their playback, communicate with them via External API, FSCommands or Variables, control external resources loading, and much more.

Flash'In'App classes enable your applications to interact with Flash Player.plugin and SWF files themselves.

Your users will need at least Mac OS X 10.4 or later and Flash Player 8 or newer to run applications that you develop with Flash'In'App. Note: transparency will only work correctly on Intel-based Macs (sorry, it's not our limitation, but Adobe's one).

Labels: ,