Reputation: 519
How do you add counts inside of a UITableView
UITableViewCell
like the iOS Mail app?
Upvotes: 6
Views: 3553
Reputation: 7560
The easiest solution would be to set an UILabel
as accessoryView
or using a custom UITableViewCell
subclass which could be designed using IB.
I'd recommend creating a simple rounded UIView
and a UILabel
as a subview in it. I'd probably create a UITableViewCell
subclass to manage the content.
Definitively the most easy way would be using a ready-to-use class like TDBadgedCell
Upvotes: 0
Reputation: 7758
Create a custom UITableViewCell, position the labels where you want them (title, subtitle, count, whatever you need). I highly recommend Matt Gallaghers custom UITableView code - it takes a lot of the headaches out of dealing with custom rows. You'll have to follow Matt Gallaghers steps for customizing the cell.
To get the appearance of the count label as close as possible to your example (mail.app), you'll have to set the UILabel backgroundColor to gray (or whatever color you want it to be), textColor to white, and layer.cornerRadius to something equal to half the height of the label (if label is 20 high, cornerRadius should be 10). This will result in a UILabel with white text, gray background, round corners. Note - this isn't the most efficient method of doing this, but Apple hasn't put up the WWDC session video where they explain the performant method better (I missed that session).
Upvotes: 0
Reputation: 2049
In addition to DDBadgeViewCell (mentioned by @micpringle), there's also TDBadgedCell.
I tried out both and found TDBadgedCell to suit my needs more, as it puts the badges over the cell's text rather than under it, meaning the badges are visible even for cells with long texts.
The project also seems to be (currently, at least) more active than DDBadgeViewCell. (That being said, there seems to be a bug in the non-ARC version of TDBadgedCell.)
Upvotes: 1