不能单击UITableview中的单元格内的UIButton

已经search了一些可能的修复,但都没有解决我的。

我不断点击uitableview中的单元格,而不是内部的button。

这里是我的代码:

- (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; } 

我也尝试了UITapGestureRecognizer的button,仍然无法正常工作。

谢谢。

我只是被这个问题困惑….

我设法通过在一个项目上完成这个问题来解决这个问题:

 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 } 

但同样的不适用于不同的项目。 我不知道为什么…

还有另外一个可能的原因就是这个问题,那就是UITableViewCells现在有一个contentView 。 这可以并将放置在您的自定义表格视图单元格中的其他视图的前面,以便选取触摸并检测单元格select。 在这样做的时候,它可能会阻挡它背后的任何视图。 当使用笔尖创build单元格时,我特别注意到了这个烦恼。

接受的方法是确保所有子视图不是添加到tableview单元格本身,而是添加到contentView 。 这是一个只读属性,可以在某些情况下调整自己的框架,例如,当侧向滑动以显示删除button或在编辑模式下时。 因此,您应该确保您添加到contentView任何项目都具有智能大小,并具有正确的大小调整行为。

另外请注意,通过在contentView添加单元格,您可以自己阻止触摸,从而防止选中单元格。 我个人尝试不允许在包含具有自定义用户启用控件的单元格的表格中select单元格。 它只会导致混乱和尴尬的界面。 事实上,我正在认真考虑用UICollectionViewsreplace未来项目中的所有UITableViews ,这些UICollectionViews更具适应性。

-灰

你的UIButton ='sy偏移量是160, UIView's高度只是120.请改变UIButton's y偏移量并尝试。

UITableViewCell默认处理所有的手势。 设置cell.selectionStyle = UITableViewCellSelectionStyleNone; 使其禁用默认行为。

我遇到了这个问题,但在我的情况下,这是不相关的表视图,而是因为我在我的button使用子视图userInteractionEnabled为true,为所有button的子视图设置为false(NO)这个问题给我。

对于Swift 3和Storyboard

对我来说,我必须在内容视图中启用多点触摸,并在所有内容上启用用户交互 – 表格视图,表格视图单元格,内容视图和UIButton。 我正在使用故事板。

我也build立了我的tableview单元格作为子类。

由于在我探索这个问题的时候我找不到答案,所以我在这个问题上发了一个关于堆栈溢出的问题。 你可以看到我的代码,并下载一个项目在这里工作: 不能点击UITableViewCell内的button?

创build一个自定义的UITableViewCell。 参考这个链接 。

假设你有一个名为displayButton的UIButton ,你可以这样做:

在你的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath ,在你创build你的单元格后,

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

在你的ViewController中创build方法如下:

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

我有同样的问题,不知何故,我发现我的错误。
在我用self(UITableViewCell)constraints改为constraints self(UITableViewCell) contentView后,我的单元格的button可以再次点击。

我正在这样做:

 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 }() 

而且它似乎调用封闭的addTarget方法不起作用。 当我将addTarget方法分解出来,并将其放入初始化程序中时,一切都奏效了!