iPhone devsugar: Simple table badges
TUAW's devsugar series helps introduce developers to tools and tricks that they might not yet be familiar with. Today's tip centers on table-level badging and how to simply and easily add badged cells to your iPhone Xcode projects.Badges are a natural partner for table view cells. When working with one-to-many data like mailboxes with letters, or RSS feeds with articles, they indicate how many items (or new items) branch off of each available choice. The standard Apple SDK does not offer badge functionality.
iPhone Developer Tim Grant Davies to the rescue. He has built an open source github repository for his TDBadgedCell project. Distributed under the Creative Commons Public License, this class allows you to add numbers to the right of each table view cell, and choosing a background color for each number.
Each badge is drawn in a custom view using CoreGraphics. This means the class does not rely on extra images stored in memory, and the badges are drawn quickly on demand. You can set color properties for each badge, indicating hues for both the normal and highlighted states.
When working with cells without accessories -- accessories are a table cell property defined in the official SDK -- the badge is placed into the accessory view. When accessories have been defined, the badge is instead placed conditionally, to the left of of any shown accessory. The screen shot here demonstrates the use of the TDBadgedCell class using a table with disclosure chevron accessories.
The class inherits directly from UITableViewCell, and can be used in its place when providing cells for a table's data source. Simply add the class to your Xcode project and return badged cells instead of standard table view cells. Use setBadgeNumber: to set the badge to an integer value.
Davies developed this class for use in his personal projects but when he found a high demand for these features from many other developers, he decided to re-write the badge cell from scratch. He has provided it as an open source class for all iPhone developers to take advantage of. So if your application could use a some badges for notification, head on over and try it out!
Share
Categories
TUAW's devsugar series helps introduce developers to tools and tricks that they might not yet be familiar with. Today's tip centers on...
Add a Comment
Tim Davies
You can follow @TDBadgedCell for updates or queries :D
January 08 2010 at 10:09 AM Report abuse Permalink rate up rate down Reply
felix
1 reply to felix's comment
Why not simply use a UILabel with layer.cornerRadius = frame.height/2 ?
January 08 2010 at 5:50 AM Report abuse Permalink rate up rate down Reply
Tim Davies
I have always drawn any shapes on iPhone in CoreGraphics. Habit I guess.
January 08 2010 at 8:13 AM Report abuse Permalink rate up rate down Reply
Jeremy
4 replies to Jeremy's comment
So is it just me, or do the terms of the license forbid actually using it in any iPhone applications?
Joe Ricioppo
This class is great. I love reading clean code.
Nice one mate.
Hot Apps on TUAW
Deals of the Day
more deals- Verizon Leather Sleeve for Tablets for $4 + free shipping
- Wicked Jaw Breaker Noise-Isolating In-Ear Headphones for $6 + free shipping
- Refurb Apple MacBook Air Laptops: 12" 64GB SSD for $699 + free shipping
- JVC Motion Sensing Clock Radio with Dual iPod Docks for $55 + free shipping
- Apple iPhone Headset with Mic for $4 + $2 s&h
- Refurb Apple iPod nano 8GB MP3 Player for $99 + free shipping, 16GB for $119
Software Updates
more updates- EFI Firmware Update brings Lion Internet Recovery to 2010-model Macs
- OS X Lion 10.7.3 released with Safari 5.1.3, Wi-Fi bug fix
- Aperture updated to 3.2.2, addresses Photo Stream issue
- Apple updates Keynote to address Lion issues
- Google Search app gets new look on iPad
- Apple releases Apple TV Software Update 4.4.3



10 Comments