UITableViewCell中的UIButton从UITableView中窃取触摸

我有一个类似于这个问题,但答案提供没有什么帮助。 我有UITableView与一些自定义的UITableViewCells ,这些单元格有一些嵌套的自定义UIViews和最后一些UIButtons里面。 如上面指定的问题,问题是,当我触摸我的button触摸事件不会填充到UITableView ,它永远不会滚动。

这是一些代码(这只是最快的方法来重现,这不是我的实际代码):

 @interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> @property (strong, nonatomic) IBOutlet UITableView * tableView; @end @implementation ViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain]; [self.view addSubview:self.tableView]; self.tableView.delegate = self; self.tableView.dataSource = self; } return self; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell * cell = [[UITableViewCell alloc] init]; UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; button.backgroundColor = [UIColor redColor]; button.frame = CGRectMake(0, 0, 50, 44); [cell.contentView addSubview:button]; return cell; } @end 

在唯一的单元格中的红色button不会让表视图反弹滚动。

任何人都可以帮我一下吗?

PS不要关注我提供的代码的愚蠢,我意识到所有关于它的问题。 我只是提供它来显示问题是什么。 这是buttonViewscrollView的冲突。

这是tableviews使用的UIScrollView的标准行为。 在你移动手指之前,系统并不知道你要滚动,但是到那时你已经“按下”了button,所以它假定你想要做什么。

你可以在你的tableview的scrollview中使用一些属性来改变行为,但是你可能会发现它们会对你的单元格和button的感觉产生负面影响,因为这会增加延迟。

 self.tableView.delaysContentTouches = YES; 

delaysContentTouches如果此属性的值为YES,则滚动视图会延迟处理向下触摸手势,直到它可以确定滚动是否为意图…

 self.tableView.canCancelContentTouches = YES 

canCancelContentTouches如果此属性的值为YES且内容中的视图已经开始跟踪触摸它的手指,并且如果用户拖动足够的手指以启动滚动,则视图接收到touchesCancelled:withEvent:消息,并且滚动视图处理作为一个滚动触摸。

您可以通过覆盖UITableView中的touchesShouldCancelInContentView:来更改行为。 为了这个工作,你需要用loadView或xib文件replace这个子类的表视图。

 @interface AutoCancelTableView: UITableView @end @implementation AutoCancelTableView // // Overriding touchesShouldCanceInContentView changes the behaviour // of button selection in a table view to act like cell selection - // dragging while clicking a button will cancel the click and allow // the scrolling to occur // - (BOOL)touchesShouldCancelInContentView:(UIView *)view { return YES; } @end 

因为以上所有答案都不适合我。 我添加一个imageView的button,并使imageView的用户界面是,然后在iamgeView上添加一个轻拍手势。 在轻拍手势相关的方法,我把button相关的方法,所以一切都很好..可能是黑客。 但是它运作良好..

下面的代码适用于我:

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ cell = [tableView dequeueReusableCellWithIdentifier:@"cell_id"]; cell.userInteractionEnabled = YES; if (cell == nil) { cell = [[[CustomCell1 alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell_id"]autorelease]; } [cell.button addTarget:self action:@selector(button_action) forControlEvents:UIControlEventTouchUpInside];} -(void)button_action{NSLog(@"Hello!");} 

这是我的自定义单元格:

 #import "CustomCell.h" @implementation CustomCell @synthesize button; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { button = [[UIButton alloc]init]; button =[UIColor redColor]; [self.contentView addSubview: button]; } return self;} - (void)layoutSubviews { [super layoutSubviews]; CGRect contentRect = self.contentView.bounds; CGFloat boundsX = contentRect.origin.x; CGRect frame; frame= CGRectMake(boundsX+50 ,+15, 100, 100); button = frame;} - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state }