How to add star rating to UITableView cell?
I need to display a bunch of UITableViewCell
in my iPhone application. Sample look below. I know how I can enable accessory view and image to UITableView cell but the prob开发者_Go百科lem is that I need to add a star rating as visible on the screen. There's no fancy logic behind it, it will just be few images that will be either enabled or disabled.
I though that I could create a custom UITableViewCell
in NIB but how would I use it in cellForRowAtIndexPath
method. If it was just one custom cell I'd just provide an outlet for it and hook it with a property and use it there. But since it would be used multiple times I think I'll need to clone it or copy somehow.
What is the simpliest way of adding such stuff to UITableViewCell?
alt text http://stuff.rajchel.pl/iphonestars.png
This Matt Gallagher post helped me a ton when dealing with custom TableView cells http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html.
Summary 1) Do not subclass UITableViewCell 2) In the cellForRowAtIndexPath method, layout your custom subviews on top of the dequeued UITableViewCell.
To add a star just use a UIButton with the default image of a unhighlighted star and a selected image of a highlighted star.
Maybe my FavStarControl is what you need? ;)
- Take 5 images in a custom cell for stars
- Make their outlets in
UITableViewCell
- create an
NSMutableArray *starArray
andCFFloat
rating inside the interface inside the
cellForRowAtIndexPath
rating = any float value that can be0,0.5,1,1.5
..... and call(void)rightImageSet
: this method will fill in all the images string you required for the particular rating.In
rightImageSet
apply the logicIn a case anything is not clear comment
I think you may find everything you need here : http://github.com/eisernWolf/TouchCustoms
Either have a look at the code to have an idea of the code you need to use or just implement directly their source code into your project (do not forget to read carefully the license ;))
But in my opinion, it should be fairly easy to make your own custom rating system :
- Make a star icon - one empty, one plain
- Create UIButtons with relevant icons
- Make sure to implement relevant touch events
Okay, that's a summary :D but it shouldn't take too much time to build ;)
精彩评论