Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to setup HCSStarRatingView view in UICollection view cell Swift? #58

Closed
PrakashMaharjan opened this issue Aug 10, 2017 · 6 comments
Closed

Comments

@PrakashMaharjan
Copy link

PrakashMaharjan commented Aug 10, 2017

Hello friends,
I am having problem using HCSStarRatingView in UICollection Cell.
How to setup HCSStarRatingView view in UICollection view cell Swift 3?
HCSStarRatingView star value set keeps on changing when rating view in UICollectionView Cell and collection view is scrolled.

I added HCSStarRatingView on UICollection View Cell in storyboard.
Created strong outlet of HCSStarRatingView as

@IBOutlet var ratingView: HCSStarRatingView!

Then in collection view cell for index path code is like this.

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = self.collectionVC.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath as IndexPath) as! CustomCollectionViewCell

cell.ratingView.value = (self.ratingValueDataArray.value(forKey: "rating") as AnyObject).object(at:indexPath.row) as! CGFloat // setting rating value from JSON array response.
return cell

}

However, for first time value is set on rating view correctly. But on collection view scroll the value is changed on random manner.

Any help?

Thanks

@hsousa
Copy link
Owner

hsousa commented Aug 10, 2017

That code should work to properly configure the rating view in a cell.

Are you sure you're not doing anything else to that cell or the rating view when the collection view scrolls?

@PrakashMaharjan
Copy link
Author

Yes, thats all i have done.
However, I got weird behavior of rating view being changing on scroll.
I am not sure what is the actual issue..

@PrakashMaharjan
Copy link
Author

Hi hsousa,
thanks for response.
I figured out the issue. It was that I had set the minimum value of star to be set as 1 in storyboard.
But as I am setting the rating UI with the values from JSON array, the some values were 0, so as minimum value it could set was 1 it showed such weird behaviours.

But I have one confusion. I am using this same rate UI in table cell to display rating done by users and also to rate from it. I allow minimum value user can set as 1 and does allow half rating. So users will not be able to rate below 1, but will be able to rate as 1.5 value. Thats working fine. Not a problem.
But I get JSON array, where it gives rating value done by users. I show the rating UI or set rating value according to those values. Initially when any user have not done any rating the values that I get is 0.0. It means not rated. So with this value I tried to set it to rating view. It means the rating UI should show no rating values. i.e all unfilled star. For this i set rating value = 0. But because, I only allow minimum value that can be set as 1. It conflicts the logic. The rating UI is not set with unfilled stars as the for this value is 0.0 which is less than minimum value that can be set i.e. 1.0. How can I make the rating UI with unfilled star when initially there is no rating done.
I also need to set the rating minimum value to 1 and max 5. But as initially if no rating case I cannot set the rating value to 0 or show the rating UI with all unfilled star. I am not able to do so.
Note that I can make the rate UI show with no filled star when i set the minimum value to 0 instead of 1. Like screenshot attached. But doing this will make user to also able to rate on rating UI with value less than 1 i.e 0.5. This is my issue right now.
How can I update the rating UI with initial value or unset value and also set the minimum value 1 the user can set/update. Is there any solution, idea you recommend for this situation?
Is there any work around to get so.

Thanks
simulator screen shot aug 17 2017 4 18 23 pm

@hsousa
Copy link
Owner

hsousa commented Aug 17, 2017

Interesting, I never really thought about the scenario you're describing, where the initial value might be different than the one allowed to be input by the user.

To be honest, this is actually something that would make sense to be added to HCSStarRatingView, and I'm planning on implementing a couple of new things for a 2.0 update later this year 🤞.

As a temporary workaround, how about this:

  1. Subclass HCSStarRatingView and override -beginTrackingWithTouch:withEvent: (see code below)
  2. Use your subclass and leave minimumValue=0, since it'll be handled automatically by the subclass.
  3. Set minimumValueAfterTouches=1
  4. Profit *
@interface CustomStarRatingView : HCSStarRatingView
@property (nonatomic) CGFloat minimumValueAfterTouches;
@end

@implementation CustomStarRatingView

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {

    // This ensures you only do this when the base class allows tracking to begin
    if ([super beginTrackingWithTouch:touch withEvent:event]) {
        self.minimumValue = self.minimumValueAfterTouches;
    }

    return NO;
}

@end

* I didn't actually test any of this 😂

@PrakashMaharjan
Copy link
Author

Thanks hsousa for the response.
I will be waiting for the 2.0 update. Hope there will update in Swift as well. 👍
Good day. :)

@hsousa
Copy link
Owner

hsousa commented Aug 18, 2017

My pleasure!

@hsousa hsousa closed this as completed Aug 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants