开发者

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? ;)


  1. Take 5 images in a custom cell for stars
  2. Make their outlets in UITableViewCell
  3. create an NSMutableArray *starArray and CFFloat rating inside the interface
  4. inside the cellForRowAtIndexPath rating = any float value that can be 0,0.5,1,1.5 ..... and call (void)rightImageSet: this method will fill in all the images string you required for the particular rating.

  5. In rightImageSet apply the logic

    How to add star rating to UITableView cell?

  6. In 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 :

  1. Make a star icon - one empty, one plain
  2. Create UIButtons with relevant icons
  3. Make sure to implement relevant touch events

Okay, that's a summary :D but it shouldn't take too much time to build ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜