如何在cellforrowatindexpath之后更新自定义表格单元格中的UIButton图像?

时间:2013-07-23 00:57:56

标签: ios image uitableview uibutton didselectrowatindexpath

我的桌面视图中有一个按钮,在点击后会发生变化。

在我的cellforrowatindexpath中,我有

 [cell.graphicButton addTarget:self action:@selector(changeGraphic:)forControlEvents:UIControlEventTouchUpInside];

应该调用changeGraphic(更改图形)。调用成功,但在changeGraphic中,图像不会改变。

- (IBAction)changeGraphic:sender
{


    buttonState++;
// button state is an int, im not using BOOl because I have more than 2 images to change from, but for this question i only included 2
    if(buttonState == 2){
        buttonState = 0;
    }
    // 1 = travel time 2 =activity time 3 = total time
    if (buttonState == 0) {



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

    if (buttonState == 1) {


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

[self.tableView beginUpdates];
[self.tableView reloadData];
[self.tableView endUpdates];

}

当我在changeGraphic方法中记录内容时,它返回正常。我是否正确更新了UIButton的图像?

1 个答案:

答案 0 :(得分:5)

应该是

[cell.vicinitimeGraphicButton setImage:[UIImage imageNamed:@"travelDistanceGraphicGreen.png"] forState:UIControlStateNormal];

而不是

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

在我的情况下,图像被创建为背景