1

编写以下代码的更好方法是什么?有一些条件代码检查 reviewStarsNumber 的 int 值,然后根据结果显示适当的 uiimage。目前它似乎很长,我想象它可以被简化。

谢谢你的帮助

  if ([reviewsStarsNumber intValue] == 0) {
    UIImage *img = [UIImage imageNamed:@"hstars-0.png"];        
    [self.reviewStars setImage:img];
}

if ([reviewsStarsNumber intValue] == 10) {
    UIImage *img = [UIImage imageNamed:@"hstars-0_5.png"];
    [self.reviewStars setImage:img];
}

if ([reviewsStarsNumber intValue] == 20) {
    UIImage *img = [UIImage imageNamed:@"hstars-1_0.png"];
    [self.reviewStars setImage:img];
}

if ([reviewsStarsNumber intValue] == 30) {
    UIImage *img = [UIImage imageNamed:@"hstars-1_5.png"];
    [self.reviewStars setImage:img];
}

if ([reviewsStarsNumber intValue] == 40) {
    UIImage *img = [UIImage imageNamed:@"hstars-2_0.png"];
    [self.reviewStars setImage:img];
}

if ([reviewsStarsNumber intValue] == 50) {
    UIImage *img = [UIImage imageNamed:@"hstars-2_5.png"];
    [self.reviewStars setImage:img];
}

if ([reviewsStarsNumber intValue] == 60) {
    UIImage *img = [UIImage imageNamed:@"hstars-3_0.png"];
    [self.reviewStars setImage:img];
}

if ([reviewsStarsNumber intValue] == 70) {
    UIImage *img = [UIImage imageNamed:@"hstars-3_5.png"];
    [self.reviewStars setImage:img];
}

if ([reviewsStarsNumber intValue] == 80) {
    UIImage *img = [UIImage imageNamed:@"hstars-4_0.png"];
    [self.reviewStars setImage:img];
}

if ([reviewsStarsNumber intValue] == 90) {
    UIImage *img = [UIImage imageNamed:@"hstars-4_5.png"];
    [self.reviewStars setImage:img];
}
if ([reviewsStarsNumber intValue] == 100) {
    UIImage *img = [UIImage imageNamed:@"hstars-5_0.png"];
    [self.reviewStars setImage:img];
}
4

8 回答 8

5

如果您重命名图像,您可以这样做:

NSUInteger number = [reviewsStarsNumber intValue]
NSString *imageName = [NSString stringWithFormat:@"hstarts-%d.png", number];
UIImage *img = [UIImage imageNamed:imageName];
[self.reviewStars setImage:img];
于 2013-01-25T16:51:19.310 回答
4
 UImage imageNamed;
 switch([reviewsStarsNumber intValue])
 {
    case 0; imageNamed = hstars0; break;
    case 10: imageNamed = hstars0-5; break;
    // 20,30...
    case 100: imageNamed = hstars5; break;

    default: break;
 }
 [self.reviewStars setImage:img];
于 2013-01-25T16:50:05.683 回答
2

1.创建 plist 文件,例如将其命名为“hstars”。放入 key = 0, 10, 20... 和 values = hstars-0_5.png, hstars-1_5.png hstars-2_5.png 的条目

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"hstars" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

2.或者

NSDictionary *dict = @{@10:@"hstars-0_5.png", @20:@"hstars-1_5.png", ....}

那么就

UIImage *img = [UIImage imageNamed:[dict objectForKey:[NSNumber numberWithInt:reviewsStarsNumber ]]];
[self.reviewStars setImage:img];
于 2013-01-25T16:58:03.910 回答
1

有几种方法,但我会说最好的选择是 switch 语句。

switch([reviewsStarsNumber intValue])
{
 case 0:
    UIImage *img = [UIImage imageNamed:@"hstars-0.png"];
    [self.reviewStars setImage:img];
    break;
 case 10:
    UIImage *img = [UIImage imageNamed:@"hstars-0_5.png"];
    [self.reviewStars setImage:img];
    break;
.
.
.
.
(etc..)
}
于 2013-01-25T16:50:44.043 回答
1

您可以使用 switch 语句:

UIImage *img = NULL;
int number = [reviewsStarsNumber intValue];
switch (number)
{
    case 0:
        img = [UIImage imageNamed:@"hstars-0.png"];
        break;

    case 10:
        img = [UIImage imageNamed:@"hstars-0_5.png"];
        break;

    // ...

    default:
        // ...
        break;
}
[self.reviewStars setImage:img];
于 2013-01-25T16:51:30.457 回答
1

您可以使用 switch 语句。

switch (expression)
{
   case match1:
        statements
        break;
    case match2:
        statements
        break;
    default:
        statements
        break;
}

或者,您可以将图像文件重命名为hstars-<0 - 100>.png并加载它们:

UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"hstars-%@.png", reviewsStarsNumber]];
[self.reviewStars setImage:img];
于 2013-01-25T16:51:31.840 回答
1

创建一个表并在该表中查找评级:

static struct
{
    int rating;
    NSString *image;
} ratings[] =
{
   { 0, @"hstars-0.png" },
   ...
   { 100, @"hstars-5_0.png" }
};

const unsigned numRatings = sizeof(ratings) / sizeof(ratings[0]);
int rating = [reviewsStarsNumber intValue];
BOOL found = NO;
for (unsigned i = 0; i < numRatings && !found; i++)
{
    if (ratings[i].rating == rating)
    {
        UIImage *img = [UIImage imageNamed:ratings[i].image];
        [self.reviewStars setImage:img];
        found = YES;
    }
}

if (!found)
{
    // Perhaps do something if the rating wasn't found?
}
于 2013-01-25T16:52:50.380 回答
1

如果您可以将第一张图片更改为hstars-0-0.png,那么此代码应该可以满足您的需求。

int value = [reviewsStarsNumber intValue] / 10

int highIndex = value / 2
int loxIndex = value % 2 == 1 ? 5 : 0

UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"hstars-%d_%d.png", highIndex, lowIndex]];
于 2013-01-26T08:42:47.617 回答