Building a grid cell for iOS

When approached with building a tabular view of data on the iPad, my first thought was there has to be a grid control somewhere out there that does this. Having spent a good portion of my development time in C# lately, I’m used to seeing all of the feature rich libraries that are either out there on Github or that can be purchased from any of the control vendors. I did happen to find several solutions that would have worked, but my problem was just not that complex. The table’s needs are very simple. I need to be able to display rows of data that are laid out in a grid.

Here is how I accomplished that:

First thing was first, subclassing UITableViewCell

[gist https://gist.github.com/ro31337/cb2f258abea438ba7184]

Then in the implementation, I’m overriding drawRect:

[gist https://gist.github.com/ro31337/fae2cc512a560bfa8e37]

So what’s going on here, is I’m allowing the cell to keep track of the X positions where the line needs to be drawn. This is stored in the columns NSMutableArray. Then when the cell is being drawn, I’m iterating the columns and drawing a line that goes from the top of the cell down to the bottom.

And then in my Controller when the cell is being created, I call addColumn with the X coordinate that I want to draw the column at.

Like I said, my requirements were super light and this has worked out really well for me so far.

Advertisements