Advertisement

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!