added button to uitableviewcell not responding
I have created a custom tableviewcell through interface builder with a label and a button. In the .h and .m file I have made outlets and actions for the button which I have connected. This cell is added to a tableview controlled by a uiviewcontroller class. However my problem is that when I tap the button, the button is not activated, however I am pushed on to the detailed view belonging to the cell. It seems like the button is behind the cell.
Any suggestions for what I have forgotten or should change?
I have created a button programatically and added as subview to the cell instead and this works, however this gives me another problem as the button is added everytime a cell is loaded.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(200.0f, 5.0f, 100.0f, 40.0f)];
//Set image
UIImage *img = [UIImage imageNamed:@"myPackage.png"];
[button setImage:img forState:UIControlStateNormal];
[img release];
[button addTarget:self action:@selector(myPackagePushed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
I would really like to use the interface builder approach - any suggestions?
Rega开发者_如何学Pythonrds
try
[cell.contentView addSubview:button];
also
UIImage *img = [UIImage imageNamed:@"myPackage.png"]; here you are not allocating memory so no need for
[img release]; because there is a possibility of crash
The button should fire the IBAction method that you have set in IB. Have you checked by inserting an NSLog or by setting a breakpoint if the method is entered?
Is the button set to be initially activated in IB?
The crux with the IB construction is that the IBOutlet gets its value when the cell is loaded from the xib. The IBOutlet will then be the button in the cell that the tableview happend to load last.
WHen you add the button programatically, you might need to have the previous added button to removeFromSuperview before you add a button again to that cell.
In my case, the problem was that the button was a subview of the background view.
Buttons assigned to the background view apparently can't respond to touches.
精彩评论