0

如果调用了特定值,我正在尝试发布特定图像,我的理解是这不起作用,因为 NSNumber 没有正确转换为 NSString,但我不确定这是否是问题。通过使用NSLog,我看到数据是通过的,比如一个评分是4.5,这样的话我想显示四星半,但是图片不贴。我已经验证了视图已链接,并且所有内容都已连接,但没有骰子。代码如下。反馈非常感谢!

- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    [self setString];

    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
    NSString *string1 = [dic objectForKey:@"name"];
    NSString *string2 = [[dic objectForKey:@"rating"]stringValue];

    NSLog(@"RATE STRING ---> %@", string2);

    if (string2 == @"0")
    {
        UIImage *img = [UIImage imageNamed:@"zerorate.png"];
        [imageView2 setImage:img];
    }
    if (string2 == @"1")
    {
        UIImage *img = [UIImage imageNamed:@"onerate.png"];
        [imageView2 setImage:img];
    }
    if (string2 == @"1.5")
    {
        UIImage *img = [UIImage imageNamed:@"onehalfrate.png"];
        [imageView2 setImage:img];
    }
    if (string2 == @"2")
    {
        UIImage *img = [UIImage imageNamed:@"tworate.png"];
        [imageView2 setImage:img];
    }
    if (string2 == @"2.5")
    {
        UIImage *img = [UIImage imageNamed:@"twohalfrate.png"];
        [imageView2 setImage:img];
    }
    if (string2 == @"3")
    {
        UIImage *img = [UIImage imageNamed:@"threerate.png"];
        [imageView2 setImage:img];
    }
    if (string2 == @"3.5")
    {
        UIImage *img = [UIImage imageNamed:@"threehalfrate.png"];
        [imageView2 setImage:img];
    }
    if (string2 == @"4")
    {
        UIImage *img = [UIImage imageNamed:@"fourrate.png"];
        [imageView2 setImage:img];
    }
    if (string2 == @"4.5")
    {
        UIImage *img = [UIImage imageNamed:@"fourhalfrate.png"];
        [imageView2 setImage:img];
    }
    if (string2 == @"5")
    {
        UIImage *img = [UIImage imageNamed:@"fiverate.png"];
        [imageView2 setImage:img];
    }
        [[self myTableView] reloadData];
}
4

1 回答 1

2

的值string2 == @"0"未定义。试试if ([string2 isEqualToString:@"0"])

此外,您可能会考虑在您返回的值之后命名您的 png 文件。然后你可以这样做:

UIImage *img = [UIImage imageNamed:string2];
[imageView2 setImage:img];

如果这不能解决问题,那么po string2在调试器中会发生什么?当你po img设置之后会发生什么,当你设置时会发生什么po imageView2

于 2013-02-25T03:48:08.760 回答