1

我在每个有图像的 tableviewcell 中都有一个 UIButton。基本上我根据另一个参数将图像设置为不同的东西。您可以忽略 totalTime/activityTimeInt 的东西,但如果有帮助,它们只是整数(比如 totaltime = 60,activitytime = 50)。以下是 cellforrowatindexpath:

    if(totalTime <= activityTimeInt){
        [cell.vicinitimeGraphicButton setImage:[UIImage imageNamed:@"travelDistanceGraphicRed.png"] forState:UIControlStateNormal];
        [cell.vicinitimeGraphicButton setFrame:CGRectMake(5,13,70,70)];
    }
    if(totalTime + 10 >= activityTimeInt){
        [cell.vicinitimeGraphicButton setImage:[UIImage imageNamed:@"travelDistanceGraphicYellow.png"] forState:UIControlStateNormal];
            [cell.vicinitimeGraphicButton setFrame:CGRectMake(5,13,70,70)];
    }
    if(totalTime >=  activityTimeInt){
        [cell.vicinitimeGraphicButton setImage:[UIImage imageNamed:@"travelDistanceGraphicGreen.png"] forState:UIControlStateNormal];
           [cell.vicinitimeGraphicButton setFrame:CGRectMake(5,13,70,70)];
    }
}
else if (!totalTime){
            [cell.vicinitimeGraphicButton setImage:[UIImage imageNamed:@"travelDistanceGraphicGreen.png"] forState:UIControlStateNormal];
    [cell.vicinitimeGraphicButton setFrame:CGRectMake(5,13,70,70)];
}
return cell;

由于某种原因,当 if 语句运行时

else if (!totalTime){
            [cell.vicinitimeGraphicButton setImage:[UIImage imageNamed:@"travelDistanceGraphicGreen.png"] forState:UIControlStateNormal];
    [cell.vicinitimeGraphicButton setFrame:CGRectMake(5,13,70,70)];
}

在不存在 totalTime 的地方,图像看起来像:没有 totalTime if 语句

当它通过其他 3 个 if 语句(使用 totalTime)时,它们看起来像:使用总时间 if 语句

如果您需要更多信息,请询问。提前致谢。

4

2 回答 2

2

您需要使用此代码

[cell.vicinitimeGraphicButton setBackgroundImage:[UIImage imageNamed:@"travelDistanceGraphicRed.png"]
                        forState:UIControlStateNormal];

自从

  1. setImage:forState:将图像设置为按钮的实际内容。例如,即使设置了按钮标题,您也看不到它,因为您已将图像设置为内容,否则您可以同时看到左侧的图像和右侧的文本,在您的情况下

  2. setBackgroundImage:forState:将图像设置为背景。在这种情况下,您可以设置标题并将其显示在图像的顶部。

请注意,您没有为按钮设置默认背景图像

于 2013-08-09T19:25:59.190 回答
0

我发现问题在于我在 customtablecell.xib 文件中设置了默认图像,而我不应该这样做。我失败了。但是,它应该是 setbackgroundimage,就像上面所说的 ayush 一样。

在此处输入图像描述

默认图像不应设置为任何内容。

于 2013-08-09T20:05:10.027 回答