问题
I have a custom cell which has two buttons that are connected to it's methods. I add the cell to the table like this:
BSOrderedItemCell *cell = (BSOrderedItemCell *)[tableView dequeueReusableCellWithIdentifier:@"OrderItemCell"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"OrderItemCell" owner:self options:nil];
cell = (BSOrderedItemCell *)[nib objectAtIndex:0];
}
And after that I set it's properties.
The cell size is the same as table's heightForRowAtIndexPath: and none of the cells labels or buttons are outside that size.
The problem is that I can select the cell, but I can't tap on the buttons. It's as if I'm tapping on a label, nothing happens. So I end up with something like this:

EDIT: Just to clarify things. The problem is not the button click methods. It's that I can't physically click the button, like there is something in the way of it, or it's disabled by some control.
回答1:
you need to do following things
- Set the tag of your button in yourcellxib. orderitemcell in your case
get the button
UIButton *btn =(UIButton*)[cell viewWithTag:]; btn.tag=indexPath.row;
3.add method to your button
[btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
now make a method
-(void)buttonClicked:(UIButton*)btn
{
NSLog(@"the button clicked is on cell number%d",btn.tag);
}
回答2:
You can try this..
[cell.button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
To avoid cell selection:
Cell's xib -> Selection -> Set as None
回答3:
Do not create IBOutlot for then in you cellForRowAtIndexpath add the method to button
[cell.button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
and perform the selector there and if you do not required row did select delegate then add
cell.userInteractionEnabled = NO;
//too
来源:https://stackoverflow.com/questions/20798475/button-inside-table-cell-not-clickable