单击UITableView单元格的“自定义”按钮时,无法识别当前单元格对象

时间:2023-02-10 21:08:24

I have created two custom button in UITableView.when i click on button1 the other button should be visible. for that i use following code. i am using UITableView grouped data type.

我在UITableView中创建了两个自定义按钮。当我点击button1时,另一个按钮应该是可见的。因为我使用以下代码。我正在使用UITableView分组数据类型。

   btnSettingButton=[[UIButton alloc]initWithFrame:CGRectMake(265, 50, 22, 22)];
[btnSettingButton setImage:[UIImage imageNamed:@"Settings Icon.png"] forState:UIControlStateNormal];
[btnSettingButton addTarget:self action:@selector(accessoryButtonTapped:withEvent:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:btnSettingButton];
[btnSettingButton setHidden:YES];



btnUncheck=[[UIButton alloc] initWithFrame:CGRectMake(265, 15, 18, 18)];

//btnUncheck=[UIButton buttonWithType:UIButtonTypeCustom];
btnUncheck.tag=indexPath.row;
[btnUncheck setImage:[UIImage imageNamed:@"NO.png"] forState:UIControlStateNormal];
     [btnUncheck addTarget:self action:@selector(buttonTapped:event:) forControlEvents:UIControlEventTouchUpInside];

[view addSubview:btnUncheck];   
[btnUncheck release];

[cell.contentView addSubview:view];

return cell;



 - (void)buttonTapped:(id)sender event:(id)event
{
CGPoint touchPosition = [[[event allTouches] anyObject] locationInView:self.tableV];
NSIndexPath *indexPath = [self.tableV indexPathForRowAtPoint:touchPosition];
if(favoriteChecked==NO)
   {


    [sender setImage:[UIImage imageNamed:@"YES.png"] forState:UIControlStateNormal];
    if(indexPath){
    [btnSettingButton setHidden:NO];
    }
    favoriteChecked=YES;


   }
   else
   {


    [sender setImage:[UIImage imageNamed:@"NO.png"] forState:UIControlStateNormal];
    [btnSettingButton setHidden:YES];
    favoriteChecked=NO;
   }


}

when i use this code it shows the setting button to the last cell of the section.

当我使用此代码时,它会显示该部分最后一个单元格的设置按钮。

please guide me how can i show settingButton to the cell which is clicked.

请指导我如何将clickButton显示到单击的单元格。

2 个解决方案

#1


1  

You only have one global variable for the btnSettingButton - of course it points to the last button you created.

btnSettingButton只有一个全局变量 - 当然它指向你创建的最后一个按钮。

You have to keep all references to all setting buttons you created, and then get the right one in your buttonTapped function. You could do it for example by adding all references to a dictionary, using the btnUncheck as a key - the btnUncheck is the sender you get in the button clicked function, so you can get the corresponding btnSettingButton from the dictionary.

您必须保留对您创建的所有设置按钮的所有引用,然后在buttonTapped函数中获取正确的引用。您可以通过添加对字典的所有引用来实现它,使用btnUncheck作为键 - btnUncheck是您在按钮单击函数中获得的发送者,因此您可以从字典中获取相应的btnSettingButton。

#2


1  

Actually I am not clearly getting you.

其实我并没有清楚地得到你。

I think you want to add a button in cell of tableview. If you click on this button , this image should be changed..

我想你想在tableview的单元格中添加一个按钮。如果单击此按钮,则应更改此图像。

Am i right?

我对吗?

#1


1  

You only have one global variable for the btnSettingButton - of course it points to the last button you created.

btnSettingButton只有一个全局变量 - 当然它指向你创建的最后一个按钮。

You have to keep all references to all setting buttons you created, and then get the right one in your buttonTapped function. You could do it for example by adding all references to a dictionary, using the btnUncheck as a key - the btnUncheck is the sender you get in the button clicked function, so you can get the corresponding btnSettingButton from the dictionary.

您必须保留对您创建的所有设置按钮的所有引用,然后在buttonTapped函数中获取正确的引用。您可以通过添加对字典的所有引用来实现它,使用btnUncheck作为键 - btnUncheck是您在按钮单击函数中获得的发送者,因此您可以从字典中获取相应的btnSettingButton。

#2


1  

Actually I am not clearly getting you.

其实我并没有清楚地得到你。

I think you want to add a button in cell of tableview. If you click on this button , this image should be changed..

我想你想在tableview的单元格中添加一个按钮。如果单击此按钮,则应更改此图像。

Am i right?

我对吗?