In the last couple of months, I've talked a lot about tables. In Beautiful Tables, I discussed how to make your tables look more attractive and in Table Tricks for iPhone OS 3.0, I updated some of that information for 3.0.
However, neither article talked about how to make your table's section heads look more attractive, and if you've got a beautifully tinted navigation bar, a carefully colored table, and multi-view cells, that gray section header will probably stand out like a sore thumb. So, how do you make it more attractive too?
Rewriting tableView:viewForHeaderInSection:
Sadly,there isn't a simple tintColor property to set for your section header. (I think there should be, but I suspect that Apple is generally trying to discourage uses of colors in tables.) Instead you have to replace the tableView:viewForHeaderInSection: method. In doing so, you must create a view that both displays your section text and does so in a color that you want.
Here's an example:
- (UIView *) tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section {
UIView* customView = [[[UIView alloc]
initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)]
autorelease];
customView.backgroundColor =
[UIColor colorWithRed:.6 green:.6 blue:1 alpha:.9];
UILabel * headerLabel = [[[UILabel alloc]
initWithFrame:CGRectZero] autorelease];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:16];
headerLabel.frame = CGRectMake(0,-11, 320.0, 44.0);
headerLabel.textAlignment = UITextAlignmentCenter;
headerLabel.text = [NSString stringWithString:@"Your Text"];
[customView addSubview:headerLabel];
return customView;
}
This is a pretty simple three-step process. First, I create the actual view for the section, drawing its background color appropriately; then I add a label with the correct text, positioned just right, and make that a subview of the section view; and finally I return the section view.
Voila! You now have a colorful section head that should match the rest of a color table.
(I'll add one more comment: note that I decided to make the alpha transparency of my section header's background color 90%. That's because of the 3.0 feature where a header sticks to the top of a table when it scrolls past. I like the three-dimensional effect that produced when you can just barely see a bit of the table beneath your stuck section head; your mileage may vary.)

The iPhone Simulator & Performance
In iPhone in Action we talk about some about using the iPhone Simulator as a testing & debugging facility for your iPhone programs. It's certainly an easy and convenient way to work on your programs. You hit command-r and with no additional work on your part, your program compiles, the Simulator starts up, and everything runs.
However, in iPhone in Action, we also talk a bit about the places where the iPhone Simulator comes up short. It includes: using the accelerometers, using the location manager (though you can do simple tests as the Simulator reports back Apple's Silicon Valley headquarters), and downloading streaming audio.
In the last two months I've been working on a card game, which I expect to write about more here when we're ready to release. It's shown me another pretty grave short-coming in the Simulator: performance. The issue? It's too good.
I mean, we all know that the iPhone is a mobile platform, and that by nature it'll ultimately be limited in its CPU capabilities. But, it's easy to forget that with all of the sexy animations and graphics that Apple pushes back and forth around the screen. Indeed, if you're just making simple use of Apple's more front-facing frameworks, you may not hit performance problems at all.
The problem arises when you start using more in-depth frameworks like Core Data and Core Graphics. You may end up with programs which work great on the Simulator but which seriously bog down when you put it out to a real iPhone or iPod Touch device.
Here were the top two performance problems that I hit when working on my card game:
The important fact that I intend to convey here is: these problems did not show up in the Simulator, only on an actual device. This was particularly frustrating for the second problem, which I discovered after a half-day or so of programming lots of different changes.
So the moral? If you're using simple frameworks, don't worry about it, But, if you're doing more complex work, every couple of compiles, send one over to an actual hardware device. The first time you hit a performance problem that didn't show up in the Simulator, you'll be happy you did.
Next Up: I'll be back after the Thanksgiving holiday, probably talking about NSSets: how they work, why you'd want to use them, and how they integrate with Core Data.
Posted by Shannon Appelcline at 03:15 PM in iPhone in Action Commentary | Permalink | Comments (0) | TrackBack (0)