UIButton没有响应UITableViewCell中的click事件

use*_*019 5 touch uibutton uitableview ios

在ios中有这种黑暗的巫术阻止我的按钮被点击.如果我没有向uitableviewcell添加按钮,并且我单击按钮,则会触发事件.但是如果按钮在uitableviewcell中,它就不会被触发,看起来表格我准备了示例代码,如果你们可以帮助我,请在xcode中创建一个简单的单视图应用程序,然后粘贴下面的代码

//GRDViewController.h

#import <UIKit/UIKit.h>

@interface GRDViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIView* container;
@property (nonatomic, strong) UIButton* button;
@property (nonatomic, strong) UITableView* tableView;
@property (nonatomic, strong) NSArray* arr;

@end
Run Code Online (Sandbox Code Playgroud)

//GRDViewController.m

#import "GRDViewController.h"

@implementation GRDViewController
@synthesize button, container, arr, tableView;

- (void)_btnTapped {
NSLog(@"TAPPED");
}

- (void)viewDidLoad 
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    self.button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [self.button addTarget:self action:@selector(_btnTapped)    forControlEvents:UIControlEventTouchUpInside];
   [self.button setTitle:@"CLICK ME" forState:UIControlStateNormal];
  self.arr = [[NSArray alloc] initWithObjects:@"123", @"456", @"678", nil];

   self.container = [[UIView alloc]initWithFrame:self.button.frame];
  [self.container addSubview:self.button];
  [self.view addSubview:self.container];
  //[self.view  addSubview:self.button];

  self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 100, 400, 400)];

 self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight |   UIViewAutoresizingFlexibleWidth;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  self.tableView.delegate = self;
  self.tableView.dataSource = self;

  [self.view addSubview:self.tableView];

self.tableView.backgroundColor  = [UIColor redColor];





}



   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"coolcell";
UITableViewCell *cell = [tableView    dequeueReusableCellWithIdentifier:cellIdentifier];

  if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:cellIdentifier];
      //cell.accessoryType = UITableViewCellAccessoryNone;

      UIButton * btn =  [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
         [btn addTarget:self action:@selector(_btnTapped)   forControlEvents:UIControlStateNormal];
      [btn setTitle:[self.arr objectAtIndex:[indexPath row]]     forState:UIControlStateNormal];
     [cell.contentView addSubview:btn];
         }

    return cell; 
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
   return [self.arr count];      
}
 @end
Run Code Online (Sandbox Code Playgroud)

请帮助我觉得这个案子应该在ios中很常见,但没有人有解决方案吗???

编辑:当点击按钮时,它会在xcode控制台中输出NSLOG消息...所以请确保...你在那里查看结果...

Abh*_*ngh 7

你没有给按钮任何控制事件.改变它UIControlEventTouchUpInside来自UIControlStateNormal


Piy*_*yap 7

而不是像UIControlStateNormalControlEvents UIControlEventTouchUpInside一样使用这样的

[btn addTarget:self action:@selector(_btnTapped) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)