我的表格 View 中有一个按钮,它应该在被点击后发生变化。
在我的 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 的图像?
最佳答案
应该是
[cell.vicinitimeGraphicButton setImage:[UIImage imageNamed:@"travelDistanceGraphicGreen.png"] forState:UIControlStateNormal];
代替
[cell.vicinitimeGraphicButton setBackgroundImage:[UIImage imageNamed:@"travelDistanceGraphicGreen.png"] forState:UIControlStateNormal];
在我的例子中,图像被创建为背景
关于ios - 如何在 cellforrowatindexpath 之后更新自定义表格单元格中的 UIButton 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17799838/