Trying to clean up some code and having issues doing so. I have a 1-5 ranking that appears in a uitableviewcell. The cell has 5 imageviews that point to a star image. There is a starYellow.png and starGrey.png. If the ranking is 3/5, the cell should set star1, star2, star3 to starYellow.png and star4, star5 to starGrey.png.
I am having issues referencing the property star%i where i is the iteration of the loop 1-5. I can’t work out how to do something like [cell setValue:starYellow forKey:[NSString stringWithFormat:@”star%i.image”, i]];
Here is the code I want to optimize..
if (book.ratingOnline) {
[cell.ratingView.layer setOpacity:1.0f];
for (int i = 1; i <= 5; i++) {
if (i == 1) {
if (book.ratingOnline >= i) { [cell.star1 setImage:[UIImage imageNamed:@"starYellow.png"]]; }
else { [cell.star1 setImage:[UIImage imageNamed:@"starGrey.png"]]; }
}
else if (i == 2) {
if (book.ratingOnline >= i) { [cell.star2 setImage:[UIImage imageNamed:@"starYellow.png"]]; }
else { [cell.star2 setImage:[UIImage imageNamed:@"starGrey.png"]]; }
}
else if (i == 3) {
if (book.ratingOnline >= i) { [cell.star3 setImage:[UIImage imageNamed:@"starYellow.png"]]; }
else { [cell.star3 setImage:[UIImage imageNamed:@"starGrey.png"]]; }
}
else if (i == 4) {
if (book.ratingOnline >= i) { [cell.star4 setImage:[UIImage imageNamed:@"starYellow.png"]]; }
else { [cell.star4 setImage:[UIImage imageNamed:@"starGrey.png"]]; }
}
else if (i == 5) {
if (book.ratingOnline >= i) { [cell.star5 setImage:[UIImage imageNamed:@"starYellow.png"]]; }
else { [cell.star5 setImage:[UIImage imageNamed:@"starGrey.png"]]; }
}
}
}
Thanks.
Add all the stars in an NSArray,
in Cell.h
Now to reference any start you could do