Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 按钮应该在单击后更改图像,但(随机)也会更改其他按钮的图像_Ios_Button_Tableview - Fatal编程技术网

Ios 按钮应该在单击后更改图像,但(随机)也会更改其他按钮的图像

Ios 按钮应该在单击后更改图像,但(随机)也会更改其他按钮的图像,ios,button,tableview,Ios,Button,Tableview,在我的CellForRowatinex中,我初始化了一个按钮(它显示在tableview的每个单元格中): 它有一个普通的图像,如果用户点击按钮,图像就会改变。我还从XML中获取button.title的内容。因此,我告诉它从网络上获得正确的标题。这可以很好地工作,但有时(并非所有时间)也会随机更改下一个单元格或下三行单元格的按钮。我不知道为什么,在使用断点/调试器运行时找不到任何东西 -(void)customActionPressed :(id)sender { UITableVie

在我的CellForRowatinex中,我初始化了一个按钮(它显示在tableview的每个单元格中):

它有一个普通的图像,如果用户点击按钮,图像就会改变。我还从XML中获取button.title的内容。因此,我告诉它从网络上获得正确的标题。这可以很好地工作,但有时(并非所有时间)也会随机更改下一个单元格或下三行单元格的按钮。我不知道为什么,在使用断点/调试器运行时找不到任何东西

-(void)customActionPressed :(id)sender
{
    UITableViewCell *owningCell = (UITableViewCell*)[sender superview];

    NSIndexPath *pathToCell = [self.tableView indexPathForCell:owningCell];

    UIImage *buttonImage = [UIImage imageNamed:@"image2.png"];

    [self doXMLParsing];
    [sender setBackgroundImage:buttonImage forState:UIControlStateNormal];
    NSString *TableText = [[NSString alloc] initWithFormat:@"%@", [[tabelle objectAtIndex:pathToCell.row] pressed1]]; 
    [sender setTitle:TableText forState:UIControlStateNormal];
    ((UIButton *)sender).enabled = NO;
}
也许有人能看出我错在哪里。如果你需要更多的代码,就这么说吧

[button addTarget:self action:@selector(customActionPressed:)
     forControlEvents:UIControlEventTouchDown];
尝试使用UIControlEventTouchUpInside而不是UIControlEventTouchDown

此外,还可以创建自定义单元格,然后可以在为按钮创建子类UITableViewCell的类中创建属性,并通过该属性更改按钮的图像

也不要更改按钮操作的图像。您可以通过tableview的didSelectRowAtIndexPath委托中的cell.yourbuttonName访问按钮,然后更改图像

尝试使用UIControlEventTouchUpInside而不是UIControlEventTouchDown

此外,还可以创建自定义单元格,然后可以在为按钮创建子类UITableViewCell的类中创建属性,并通过该属性更改按钮的图像


也不要更改按钮操作的图像。您可以通过单元格访问按钮。您可以在tableview的didSelectRowAtIndexPath委托中选择yourbuttonName,然后更改图像。

您可以进一步解释第二部分吗?我想我并没有真正理解它。当你创建一个customcell时,你可以通过[cell.nameofyourbutton setImage:[UIIMAGE ImageName:@”“]访问它;如果你的单元格中有多个项目,最好是自定义,因为这样更容易进行更改和访问哦,好吧,是的,我不理解。但即使它是appdelegate,它也不知道如何处理“cell.mybutton”。mybutton是自定义单元格类中uibutton的对象名是的,我知道,但我无法与它交互。总是说找不到方法。你能进一步解释一下第二部分吗?我想我并没有真正理解它。当你创建一个customcell时,你可以通过[cell.nameofyourbutton setImage:[UIIMAGE ImageName:@”“]访问它;如果你的单元格中有多个项目,最好是自定义,因为这样更容易进行更改和访问哦,好吧,是的,我不理解。但即使它是appdelegate,它也不知道如何处理“cell.mybutton”。mybutton是自定义单元格类中uibutton的对象名是的,我知道,但我无法与它交互。总是说找不到方法。
[button addTarget:self action:@selector(customActionPressed:)
     forControlEvents:UIControlEventTouchDown];