我正在使用此代码更改UIButton
我的UITableView
. 问题是当我滚动表格的按钮时,没有任何理由发生变化。
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(232, 88, 78, 24)];
if([[array objectAtIndex:(indexPath.row)] isEqualToString:@"0"])
[button setImage:[UIImage imageNamed:@"pic1"] forState:UIControlStateNormal];
else
[button setImage:[UIImage imageNamed:@"pic2"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[Cell.contentView addSubview:button];
这是buttonClicked:
方法:
-(void)buttonClicked:(id)sender
{
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:self.myTableView];
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:touchPoint];
UIButton *button = (UIButton *)sender;
if([[array objectAtIndex:indexPath.row] isEqualToString:@"0"])
{
[button setImage:[UIImage imageNamed:@"pic2"] forState:UIControlStateNormal];
[array replaceObjectAtIndex:indexPath.row withObject:@"1"];
}
else
{
[button setImage:[UIImage imageNamed:@"pic1"] forState:UIControlStateNormal];
[array replaceObjectAtIndex:indexPath.row withObject:@"0"];
}
}