Creating a grid view in iOS

Creating a table in iOS is easy, but there are times when you need a grid instead of a simple table. I’ll walk you through the steps for making a grid. In this example, I want a table that has three cells per row. The final product will look like this:

GridTableViewCell

The core piece in your  grid is a custom UITableViewCell. Start off by making a new class derived from UITableViewCell. I called mine GridTableViewCell. I want my grid to have 3 cells per row, so my interface is going to look like this:

@interface GridTableViewCell : UITableViewCell {
	UIColor *lineColor;
	BOOL topCell;
	UILabel *cell1;
	UILabel *cell2;
	UILabel *cell3;
}
@property (nonatomic, retain) UIColor* lineColor;
@property (nonatomic) BOOL topCell;
@property (readonly) UILabel* cell1;
@property (readonly) UILabel* cell2;
@property (readonly) UILabel* cell3;
 
@end

The UILabels are created in initWithStyle and are sized to fit within each cell. lineColor is self-explanatory. topCell is a bit trickier. Our line drawing code (see below) draws the vertical lines and a horizontal line at the bottom of the cell. However, this leaves the top cell in the table without a line on top. To work around this, we add a flag to the cell to indicate whether the cell is the top cell. If it is, draw a line on the top of the cell.

We do our line drawing in drawRect. In the implementation for GridTableViewCell I have overridden the drawRect method and added the line drawing code. Here is what it looks like:

#define cell1Width 80
#define cell2Width 80
#define cellHeight 44
 
- (void)drawRect:(CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextSetStrokeColorWithColor(context, lineColor.CGColor);       
 
	// CGContextSetLineWidth: The default line width is 1 unit. When stroked, the line straddles the path, with half of the total width on either side.
	// Therefore, a 1 pixel vertical line will not draw crisply unless it is offest by 0.5. This problem does not seem to affect horizontal lines.
	CGContextSetLineWidth(context, 1.0);
 
	// Add the vertical lines
	CGContextMoveToPoint(context, cell1Width+0.5, 0);
	CGContextAddLineToPoint(context, cell1Width+0.5, rect.size.height);
 
	CGContextMoveToPoint(context, cell1Width+cell2Width+0.5, 0);
	CGContextAddLineToPoint(context, cell1Width+cell2Width+0.5, rect.size.height);
 
	// Add bottom line
	CGContextMoveToPoint(context, 0, rect.size.height);
	CGContextAddLineToPoint(context, rect.size.width, rect.size.height-0.5);
 
	// If this is the topmost cell in the table, draw the line on top
	if (topCell)
	{
		CGContextMoveToPoint(context, 0, 0);
		CGContextAddLineToPoint(context, rect.size.width, 0);
	}
 
	// Draw the lines
	CGContextStrokePath(context);
}

The UILabels are created in the cell’s init function. The code to create the labels looks like this:

	cell1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell1Width, cellHeight)];
	cell1.textAlignment = UITextAlignmentCenter;
	cell1.backgroundColor = [UIColor clearColor]; // Important to set or lines will not appear
	[self addSubview:cell1];

The final important task to handle in the cell is to redraw the cell when its topCell state changes. Since we are reusing cells in our table view, the cell does not always get redrawn and we can end up with an extra top line where there should not be one. The remedy to this problem is straightforward – override the setter function for the topCell property.

- (void)setTopCell:(BOOL)newTopCell
{
	topCell = newTopCell;
	[self setNeedsDisplay];
}

UITableView

Now that we have our custom table cell, let’s use it in our TableView. All the magic happens in cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
 
    GridTableViewCell *cell = (GridTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[GridTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.lineColor = [UIColor blackColor];
    }
 
    // Since we are drawing the lines ourself, we need to know which cell is the top cell in the table so that
    // we can draw the line on the top
    if (indexPath.row == 0)
        cell.topCell = YES;
    else
        cell.topCell = NO;
 
    // Configure the cell.
    cell.cell1.text = [NSString stringWithFormat:@"%i", indexPath.row];
    cell.cell2.text = [NSString stringWithFormat:@"%i", indexPath.row];
    cell.cell3.text = @"Sample text";
 
    return cell;
}

The final task is to turn off the table view’s default lines. This is done in viewDidLoad in our view controller:

- (void)viewDidLoad
{
	[super viewDidLoad];
	// Turn off the tableView's default lines because we are drawing them all ourself
	self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

Wrap-Up

That’s all there is to it for making a grid view. I have posted a demo project on GitHub – GridTableView.

This Week in Mobile and Web 03.11.11

if you’re waiting in line for an iPad 2, save me a spot.

This week — and this year, for that matter — there has been a lot of talk about ‘mobile payments’.  Many of the top companies (read: banks) are scrambling to catch up, as startups like Square and Venmo have pretty much revolutionized the way that B2C — and even C2C — transactions are made.

Square was in the news this week, boasting “$1 million mobile payments per day”.  Which didn’t sit well, with the top credit card processor, VeriFone.  Here is the story, told with links.

And now, the rest of the news…

    Design // Develop

    OS News

    The Web

    Apps, Apps, and Additional Apps

    Device Central

    Carrier News

    Friday humor

    Git Beginner Links

    As a new Recursive Awesome employee, I’ve only been using Git regularly for a few weeks now. At previous jobs I’ve used SVN, and before that CVS, so I thought myself an old hat when it comes to source control, but Git has forced me to learn some new tricks. I have found the following resources to be extremely helpful in my transition to using Git exclusively:

    1. Git – SVN Crash Course – http://git.or.cz/course/svn.html – This tutorial on getting started with Git was written for people already familiar with SVN. It has side-by-side comparisons of Git and SVN commands, with explanations. Definitely very helpful.
    2. A Successful Git branching model – http://nvie.com/posts/a-successful-git-branching-model/ – This was actually not new to me here at RA, but because it was written for Git, I had basically read it once, and not referred to it again. Now that I am actually using Git, I have found myself referring to the examples in this article at least once or twice a week.
    3. git hub – http://github.com/ – git hub recently threw this cute and helpful graphic up at the top of my dashboard, basically letting me know that there are detailed tutorials for several common git tasks. Though I have needed help with git quite a bit these last weeks, I have managed to perform all these tasks without much help. Kudos definitely go to git hub for their excellent contextual help when you first sign up for the site, and when you create a new repository, or even just browsing other people’s repositories. Often this help text contains the actual git commands you need to run to perform these everyday tasks. For posterity, however, here are the (also excellent) help docs that git hub linked to in the graphic above:
    4. git ready – http://gitready.com/ – This site is a gem. There are basically just a bunch of articles about Git here. Many are task-specific howto-type articles, but just as many are just things you should know about Git in general. This is probably not the first resource you should turn to if you’re JUST starting out, but I’ve found more than one search for help has led me to one of these informative articles.
    5. It should go without saying, but one of the best resources is of course Git itself.
      % git --help

    I am happy to report that learning to use Git has simultaneously been one of the most frustrating and one of the most enjoyable and rewarding aspects of starting out here at Recursive Awesome.

    Win an iPad 2 or Motorola XOOM at Mobile March!

    That’s right, folks.  Recursive Awesome is giving away an Android 3.0 powered Motorola XOOM and a yet-to-be-released Apple iPad 2 at Mobile March Twin Cities.

    The Motorola XOOM has only been out for a couple of week, and it’s already making its mark in the world of tablets.  I was able to get my hands on a review unit.  You can read what I think about it here.

    Complete with front and rear-facing cameras, the iPad 2 is sure to take the market by storm…again.  It will be released on March 11th, and we’ll be there to snatch one up, just for you.

    Not sure what Mobile March is all about?  Take a peek at this blog post from Recursive Awesome founder, and Mobile March organizer, Justin Grammens.  If you can’t make it to the full event, check out Mobile 3D (Demos, Dinner and Drinks).  Mobile 3D is a new addition to Mobile March.  Mobile 3D is included with your Mobile March admission, or can be purchased separately.

    See you there!

    This Week in Mobile and Web – 03.04.11

    Apple had some big new this week, with the announcement of the iPad 2, iOS 4.3, and new iPad apps.  The iPad 2 is slimmer — by a third — has front and rear facing cameras, a dual core A5 processor, and 512MB of RAM.  It also has a built-in magnet system that allows for covers and cases to ‘snap on’ and take advantage of the new lock/unlock features.  The iPad 2 will be available in WiFi and 3G versions (AT&T and Verizon), starting March 11th, starting at $499.  In addition, they announced that iOS 4.3 will be launching March 11th, to coincide with the launch of the iPad 2.  This looks to be true as iOS 4.3 “Gold Master” has been made available to developers.  It will be available for the iPhone 3Gs, iPhone 4, iPod Touch 3rd/4th gen, and both iPads.  One last thing…they also announced that iMovie and Garage Band will be available in iPad flavors soon.  You can watch the full keynote here.

    Now…the rest of the news.

    Development

    OS News

    Also, this week in the OS battle….

    Nielsen released a couple of charts depicting smartphone market share per OS, per manufacturer.  The first chart shows that there is still a three-way battle between Android, iOS, and RIM (Blackberry), with Android leading, by a slight margin.  The second show OS adoption by age.  Android appears to be more popular with younger folks.  This might have something to do with the fact that there are more devices to choose from, at lower price-points.

    The Web

    Apps, Apps, and Additional Apps

    Device Central

    Carrier News

    Friday Video

    Cult of Jobs v. Google Droid Army: The Google-Apple Wars

    Motorola XOOM; One Tablet to Rule Them All [REVIEW]

    Okay.  Maybe I’m being a little dramatic.  However, based on my time with the XOOM, and my previous experience with the iPad, I think the XOOM, with Android 3.0 (Honeycomb) has much more potential than the iPad.

    Let’s dig in…

    The Guts

    The XOOM is packing the latest and greatest in mobile technology.  It has an NVIDIA Tegra2 1GHz dual-core processor, 1GB DDR2 RAM, and 32GB of on-board storage.  It also has a microSD slot, but won’t be enabled until a future software update.  There are two cameras; Rear 5MP with dual LED flash, Front 2MP.  It also records 720p HD video.  Both picture and video quality are on par with smartphones with the same specs.  in addition to recording in HD, the XOOM will playback 1080p HD videos.  There is an HDMI port on the bottom, but I’m unsure of its capabilities, as I didn’t have a cable.  Flanking the HDMI port, you’ll find micro USB and charging ports.  On a side note, the tablet will NOT charge via the USB port.  There are also metal, dock contacts on the bottom.  The only physical buttons on the device are the volume and power/unlock buttons.  The power/unlock button is on the back of the device, next to the camera.  It’s unintuitive at first, but you get used to it.  The XOOM is 4G capable…eventually.  Unfortunately, this upgrade cannot be performed with a simple software update.  You have to box it up and ship it off to Verizon.  Not an ideal upgrade.

    (Screen) Size Matters…or does it?

    You don’t have to be a mathematician to know that 10.1″ is larger than 9.7″.  However, when you actually measure the screens, the iPad and XOOM have roughly the same screen real estate.  The XOOM merely has a wider screen, so the total diagonal measurement is larger.  There are no physical UI buttons on the XOOM.  However, there is a persistent ”System Bar” visible on the bottom of the screen, which includes the BACK, HOME, and RECENT APPS soft buttons.  It also display Notifications, the time, connectivity and the battery meter.  As a developer, it doesn’t appear that you can hide it.  You can only enable “lights out mode” to dim the icons and information…but the bar is still there.  Additionally, developers have the option to use the built-in Action Bar, which give them a consistent location to place navigation, search, and context sensitive elements.  The stock Honeycomb Home app uses this.  Add up the extra space that the “bars” take up and 10.1″ is less than 9.7″.  Regardless, the screen size is fine.

    Apps – or – The Lack Thereof

    Google did a good job of retooling their apps to utilize the larger screen. Gmail, Calendar, Contacts, Books, Maps, Market, Music, Talk, and YouTube  all received the tablet treatment.  They even added a new core app called Movie Studio.  As the name suggests, Movie Studio is a movie editing app, which allows you to combine/trim video files as well as add images, transitions, and a music score.  Then you can export your video for future upload.  While it’s nice that the core apps have been tabletized, most of your favorite apps, in the Android Market, have not.  Google does now offer a “Apps for Tablets” section, but there are only 16 apps listed.  It’s possible that these are “featured” apps, and not the entire list of apps that are available.  Rest assured, most of your favorite apps will still work on  the XOOM.  They just might not fill the screen or look quite right.  It would have been nice if Google would have reached out to some key developers to have them upgrade their existing apps to be tablet-friendly.  At the time of writing this, Adobe Flash is not present on the XOOM.  The official announcement word is that it will be “available soon, as a free download” from the Android Market.

    Pricing and Availability

    The Motorola XOOM tablet is available now through Verizon Wireless or an authorized retailer.  You can purchase it for $599.99, with a new two-year contract, or $799.99, without a contract.  Data plan options are 1GB/$20, 3GB/$35, 5GB/$50, and 10GB/$80.  4G pricing is unknown at this time.  There are several accessories available, including a standard dock, an HD speaker dock, a protective case, and a wireless keyboard (not yet available on VZW’s site, but is available at Best Buy).

    In Conclusion, and In Summary…

    Overall, I like the XOOM.  Android 3.0 is very responsive.  The updated core apps and widgets are well designed for tablet use.  The other tablet apps, that are available in the Market, also work well.  The lack of SD card and Adobe Flash capabilities, out of the box, coupled with the 4G LTE mail-in upgrade puts the the XOOM on my “wait to purchase” list.  At least until they get this sorted out.  The other reason I would wait, to purchase this tablet, is to see what else is going to be available in the next two months.  LG, Samsung and a few other manufacturers have announced new tablets.  More competition may drive prices down.

    Made with Movie Studio

    Short HD Video Sample

    Sample Pictures & Screenshots

    This Week in Mobile and Web – 02.25.11

    This week, Verizon Wireless and Motorola released the XOOM Android tablet.  In my opinion, this is the first Android 3.0 (Honeycomb) tablet that will give the iPad a run for it’s money.  It has a 10.1″ screen, NVIDIA Tegra 2 1GHz dual-core processor, 1GB RAM, 32GB on-board storage, 5MP back cam with flash, 2MP front cam, HDMI, 720P video recording and playback, and a ton of other features (full specs).  As far as I can tell, Motorola and Verizon have not altered the operating system in any way.  There are no V-Cast or MotoBLUR apps preinstalled.  Google has updated the core apps for tablet consumptions; Gmail, Calendar, Browser, Camera, Gallery, Music.  They’ve even included Movie Studio, which allows you to splice, trim, fade, title, score and add photos to your recorded videos.

    Keep an eye out for my full review, coming soon.

    Development

    The Web

    Apps, Apps and Additional Apps

    Carrier News

    Device Central

    Friday Video

    Friday Infographic

    This Week in Mobile and Web – 02.17.11

    You may have noticed the lack of TWIMAW.  We’ve been busy around the office, kicking off new projects, working on existing ones, and hiring a bunch of people. Let’s call this edition “The Last Few Weeks in Mobile and Web”.

    Mobile World Congress

    Last month, I had the chance to go to the Consumer Electronics Show (CES).  While CES is typically an audio/video conference, there were plenty of ‘mobile’ announcements.  This month, Mobile World Congress (MWC) was held in Barcelona.  As the name would indicate, MWC is all about mobile.  Several manufacturers took this opportunity to announce their 2011 line-up.

    HTC had a pretty big line up, not only announcing phones, but a tablet as well; The HTC Flyer.  It’s a 7″ tablet, with a 1.5GHz processor, 32GB of internal storage, with front and rear cameras.  In addition to the tablet, they announced five phones!  HTC Cha-Cha, HTC Salsa, HTC Incredible S, HTC Desire S, and HTC Wildfire S.  The Cha-Cha, Salsa and Wildfire S are entry level phones, with smaller screens, 600MHz processors and very little internal storage.  The Cha-Cha and Salsa are unique in that they have a Facebook button that allows for instant access to publishing photos & videos, reading updates and even Facebook Chat.  The Incredible S and Desire S are mid-grade devices, with 1GHz processors, ~1GB internal storage and 4″ and 3.7″ screens respectively.  Throw the HTC Thunderbolt and HTC Inspire — announced at CES — and you have to wonder where HTC’s dual-core offerings are.

    Other MWC Links

    Development

    The Web

    Apps

    Carrier News

    Device Central

    Friday Humor/Infographic

    The Next Generation of Android Phones

    It all started with the T-Mobile G1 in 2008; The first Android powered smartphone.  3.2″ screen, 528 MHz processor, 192 MB RAM, 256 MB ROM, QWERTY keyboard.  It was a very underpowered device, but it set the wheels in motion.  The Android revolution had begun.

    Fast-forward to 2010.  Google releases the Nexus One.  The N1 was intended to be a “reference” phone.  It had the latest hardware (1GHz processor, 512 MB RAM and ROM, 5 MP Camera), a larger, 3.7″, AMOLED screen with a higher resolution, and it ran vanilla Android.  This phone (along with the previously released Motorola DROID) signified the start of “Gen 2″ in the Android universe.  Throughout 2010, many manufactures followed suit, releasing phones with similar specs to the Nexus One.  Some had larger screens.  Some with QWERTY keyboards.  Heck, a couple even saw a bump in processing power, to 1.2GHz.  Never-the-less, they were all still pretty much Gen 2.

    Fast-forward to present day…well, a few weeks ago.  Consumer Electronics Show 2011 (CES) opens its door to the public.  Traditionally, CES was all about audio and video products.  Over the past decade, computers and other tech gear has started to infiltrate the show floor.  Mobile phones and tablets have now carved out their own space.  Manufacturers and carriers alike are using CES to announce upcoming products and services.  Most of the phones shared some common attributes; larger screens, 4G technology, front-facing camera.  But just a few stood above the rest.  What makes these phones so special? Dual-core processors.

    Motorola stole the show by announcing, not one, but two phones that feature dual-core processors.  The Atrix 4G and the DROID Bionic.  The Atrix 4G will be available on AT&T and features a 4″ qHD screen (540×960 @ 240 dpi), 1GHz NVIDIA Tegra 2 dual-core processor, 1 GB RAM, 5 MP rear camera and a .3 MP front-facing camera.  Additionally, the Atrix 4G will have a whole host of accessories, including the standard-issue home and car docks.  It will also have two specialty docks.  The Laptop Dock has a large screen and keyboard.  It gives you access to Firefox web browsing, as well as the ability to still access your apps and make calls.  The HD Multimedia dock allows you to connect up to 3 USB devices, like a keyboard.

    As the name suggests, the DROID Bionic is part of the Verizon Wireless’ DROID series.  It has the same processor as the Atrix 4G.  However, it only has 512 MB ram.  Other differences include a 4.3″ screen (still qHD), 16 GB internal storage, and an 8 MP camera (with the front cam).  It doesn’t look like the Bionic have the same accessory mix as its brother.  I’d be willing to bet that there will be basic home and car docs.  One neat feature of the Bionic is Mirror Mode.

    Multiply your fun with Mirror Mode, which lets you enjoy all of your favorite apps, websites, videos, photos, and games full-screen on your larger home TV.

    This is something that I’ve been looking for in a phone.  It will be great to be able to demo apps for clients or for presentations.

    Motorola isn’t the only manufacturer throwing their hat into the dual-core ring.  LG announced the Optimus 2X some time ago and Samsung is teasing their next generation phone, just ahead of Mobile World Congress.

    With Mobile World Congress and CTIA just around the corner, I expect that most of the hottest new phones will rock dual-core processors.  Rumor has it that there will be quad-core phones out by December of this year.  I’ll believe it when I see it.

    Expanded Mobile March Event Set for March 19, 2011

    Minneapolis, MN – Organizers of Mobile March today announced the agenda for it’s second annual mobile event will be held on March 19, 2011 at the Best Buy Corporate Campus. The name Mobile March denotes not only the month in which it takes place, but also emphasizes the ongoing advancement and growth of mobile technology and the related lifestyle. Its dual track format includes more sessions than last year and will continue to offer education and information in both the Mobile Development and Mobile Business areas. In addition attendee registration includes access to Mobile March’s Mobile 3D: Demos, Dinner and Drinks featuring locally developed mobile technology the preceding night, March 18th, at CoCo in downtown St. Paul.

    According to Mobile March’s Justin Grammens, “Mobile continues to grow rapidly and offers so much opportunity for developers and business. We feel we’ve expanded and increased the value of Mobile March significantly.” According to Mobile March’s Phil Wilson, “From President Obama down, with all of the talk about innovation, we believe mobile represents a clear example of that innovation and that the Twin Cities is home to leaders in both mobile development and use.”

    The agenda, available online at mobilemarchtc.com, includes Mobile Development sessions addressing the development of applications for the new Windows 7 Phone with Microsoft’s Jeff Brand, HTML 5 and the Mobile Web with Mark Nutter from Area Studios, 4G, as well as an iPhone – Android Showdown with Recursive Awesome’s own Phil Olson and Rory Lonergan.

    The non-technical Mobile Business track includes Cracking the Code: QR Codes and Coupons with MixMobi’s Lisa Foote and guests, Location Based Services: Mayors to Marketing with Christopher Lower of Sterling Cross Communications, and Grill Yourself, a session about what you need to know when developing a mobile app for business. The track also includes Tablet’s Mean Business with Pioneer Press Tech Writer and author Julio Ojeda-Zapata and to end off the day, an excellent session entitled On the Campaign Trail With Mobile Technology by Mark Jenkins Marquis Mobile Solutions.

    The Landscape of Mobile Discussion with Pearson VUE’s Peter Pascale and a Keynote to be announced soon will target both Development and Business attendees.

    Registration is now open for Mobile March!

    Register for Mobile March via the website at www.mobilemarchtc.com or directly through http://mobilemarchtc.eventbrite.com

    Event sponsors include: Recursive Awesome, RemainComm, Focus Business Development, Best Buy, Verizon Wireless, Fusion Room and Microsoft.