I have a button in my tableview that is supposed to change after it is tapped.

In my cellforrowatindexpath, I have

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

which is supposed to call changeGraphic (which changes the graphic). The call is successful, but in changeGraphic the image doesn't change.

- (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];

}

When I log stuff inside the changeGraphic method, it returns fine. Am I updating the images of the UIButton correctly?

有帮助吗?

解决方案

it should be

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

instead of

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

in my case, the image was being created as background

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top