Cannot click a UIButton inside a cell in a UITableview

I was already looking for some possible corrections, but mine did not solve everything.

I keep clicking on a cell in a uitableview, not the buttons inside it.

here is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.backgroundColor = [UIColor blackColor]; UIView *v = nil; NSArray *array = [cell subviews]; for (v in array) { NSLog(@"%@",[v class]); if( [v isKindOfClass:[UIView class]] ){ [v removeFromSuperview]; } } //tb if (tableView == self.tb) { v = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.tb.bounds.size.width, 120.0)]; if([feeds count]>0){ UIImageView *box=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"statusBox.png"]]; box.userInteractionEnabled = YES; [v addSubview:box]; AsyncImageView *imageView1 = [[AsyncImageView alloc] initWithFrame:CGRectMake(10, 20.0f, 34.0f, 34.0f)]; imageView1.contentMode = UIViewContentModeScaleAspectFill; imageView1.clipsToBounds = YES; //cancel loading previous image for cell [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:imageView1]; if (users1 != nil && users1.imagelink != nil && (id) users1.imagelink != [NSNull null]){ imageView1.imageURL = [NSURL URLWithString:users1.imagelink]; } else{ imageView1.image=[UIImage imageNamed:@"default_ProfilePic.png"]; } UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myFunction:)]; tapped.numberOfTapsRequired = 1; [imageView1 addGestureRecognizer:tapped]; [tapped release]; imageView1.userInteractionEnabled = NO; [v addSubview:imageView1]; UIFont *font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:11]; UILabel *descLabel1 = [[UILabel alloc] initWithFrame: CGRectMake(52, 23, 160, 48)]; descLabel1.text = [NSString stringWithFormat:@"%@ %@",users1.userfirstn,users1.userlastn]; descLabel1.textColor = [UIColor blackColor]; descLabel1.font = font; descLabel1.adjustsFontSizeToFitWidth=YES; descLabel1.numberOfLines = 0; CGSize expectedLabelSize = [descLabel1.text sizeWithFont:descLabel1.font constrainedToSize:descLabel1.frame.size lineBreakMode:UILineBreakModeWordWrap]; CGRect newFrame = descLabel1.frame; newFrame.size.height = expectedLabelSize.height; descLabel1.frame = newFrame; descLabel1.numberOfLines = 0; descLabel1.userInteractionEnabled = NO; [v addSubview:descLabel1]; UILabel *descLabel2= [[UILabel alloc] initWithFrame: CGRectMake(52, 43, 200, 48)]; StatusClass *stat1=[feeds objectAtIndex:indexPath.row]; descLabel2.text = [stat1 statcreate]; descLabel2.textColor = [UIColor blackColor]; descLabel2.font = font; descLabel2.adjustsFontSizeToFitWidth=YES; descLabel2.numberOfLines = 0; CGSize expectedLabelSize2 = [descLabel2.text sizeWithFont:descLabel2.font constrainedToSize:descLabel2.frame.size lineBreakMode:UILineBreakModeWordWrap]; CGRect newFrame2 = descLabel2.frame; newFrame2.size.height = expectedLabelSize2.height; descLabel2.frame = newFrame2; descLabel2.numberOfLines = 0; descLabel2.userInteractionEnabled = NO; [v addSubview:descLabel2]; UILabel *descLabel3= [[UILabel alloc] initWithFrame: CGRectMake(10, 63, 280, 80)]; StatusClass *stat=[feeds objectAtIndex:indexPath.row]; descLabel3.text = [stat stattext]; descLabel3.textColor = [UIColor blackColor]; descLabel3.font = font; descLabel3.adjustsFontSizeToFitWidth=YES; descLabel3.numberOfLines = 0; descLabel3.userInteractionEnabled = NO; [v addSubview:descLabel3]; //comment button UIButton *buttonC = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [buttonC addTarget:self action:@selector(sendComment:) forControlEvents:UIControlEventTouchUpInside]; [buttonC setImage:[UIImage imageNamed:@"comment.png"] forState:UIControlStateNormal]; buttonC.frame = CGRectMake(2, 160, 145, 35); buttonC.userInteractionEnabled = YES; [v addSubview:buttonC]; //share button UIButton *buttonS = [UIButton buttonWithType:UIButtonTypeRoundedRect]; buttonS.tag = indexPath.row; [buttonS addTarget:self action:@selector(sendShare:) forControlEvents:UIControlEventTouchUpInside]; [buttonS setImage:[UIImage imageNamed:@"share.png"] forState:UIControlStateNormal]; buttonS.frame = CGRectMake(150, 160, 140, 35); buttonS.userInteractionEnabled = YES; [v addSubview:buttonS]; } v.userInteractionEnabled = YES; [cell addSubview:v]; } return cell; } 

I also tried UITapGestureRecognizer for buttons and still didn't work.

Thanks.

+7
ios iphone uitableview uibutton uitapgesturerecognizer
source share
9 answers

I am just puzzled by this problem ....

I managed to solve this problem by doing this in one project:

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("RegCell", forIndexPath: indexPath) as! CustomCell cell.contentView.userInteractionEnabled = false // <<-- the solution return cell } 

but the same did not work for another project. I have no idea why ...

+21
source share

There is another potential cause for such problems, namely UITableViewCells now has a contentView . This can and will be placed in front of other views in your custom cells of the table view in order to select strokes and determine the choice of the cell. At the same time, he can block touching any species behind him. I especially noticed this annoyance when creating cells with knives.

The accepted way to deal with this is to ensure that all subitems are not added to the tableview cell itself, but to its contentView . This is a read-only property that, under certain circumstances, adjusts its own frame, for example, when it slides to the side to open the delete button or in edit mode. Therefore, you must make sure that all the elements you add to the contentView are of reasonable size and have the correct resizing behavior.

Also keep in mind that by adding cells to the contentView , you can lock it yourself, and thus prohibit cell selection. I personally try not to allow the selection of cells in table views that contain cells with custom user controls. This only leads to confusion and inconvenient interfaces. In fact, I'm seriously considering replacing all of the UITableViews in my future projects with UICollectionViews , which are much more adaptable.

-Ash

+18
source share

Your UIButton = y offset is set to 160, and UIView's height is set to 120. Change the UIButton's y offset and try.

+4
source share

For Swift 3 and storyboard

For me, I had to enable multiple touch inside the content view and enable user interaction in everything: table view, table cell, view content and UIButton. I used the storyboard.

I also set the tableview cell as a subclass.

Since I could not find the answer here when I was researching the problem, I posted another stack overflow question about this. You can see my code and download the project with this here: Can't click a button inside a UITableViewCell?

+4
source share

UITableViewCell by default performs all gestures. Set cell.selectionStyle = UITableViewCellSelectionStyleNone; to disable the default behavior.

+3
source share

I ran into this problem, but in my case it was not related to the table view, instead it was because I used subviews in my button for which userInteractionEnabled was true, setting this value to false (NO) for all the sub-items of the buttons fixed the problem for me.

+2
source share

Create a custom UITableViewCell. Refer this link .

Say you have a UIButton named displayButton, you can do something like this:

Inside - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath , after creating the cell,

 [cell.dislayButton addTarget: self action: @selector(replyButtonPressed:withEvent:) forControlEvents: UIControlEventTouchUpInside]; 

And create such a method in your ViewController:

 - (void) replyButtonPressed: (id) sender withEvent: (UIEvent *) event { UITouch * touch = [[event allTouches] anyObject]; CGPoint location = [touch locationInView: self.tableView]; NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: location]; } 
0
source share

I have the same problem and somehow I realized my mistake.
After I changed the constraints to self(UITableViewCell) to constraints using the contentView of self(UITableViewCell) , my cell button will be clicked again.

0
source share

I did it:

 let plusButton: UIButton = { let v = UIButton() v.setImage(plusImg, for: .normal) v.tintColor = .dark v.translatesAutoresizingMaskIntoConstraints = false v.addTarget(self, action: #selector(plusButtonHandler), for: .touchUpInside) // adding targets here did not work return v }() 

And it looks like the addTarget method inside closure is not working. When I used the addTarget method and instead put it in the initializer, everything worked!

0
source share

All Articles