2012-05-24 52 views
5

ios中有这样的黑暗魔法阻止了我的按钮被点击。如果我不向uitableviewcell添加按钮,并且我单击按钮,则会触发该事件。 ,但如果按钮在uitableviewcell中,它不会被触发,它似乎表 我有示例代码准备好了,如果你们可以帮助我,请在xcode中创建一个简单的单视图应用程序,只需粘贴以下内容代码UIButton没有响应UITableViewCell中的点击事件

//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 

//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 

请帮助 我觉得这种情况应该是IOS常见,但一直没人解决FO这个?

编辑:当点击该按钮它打印的NSLog味精在Xcode控制台......所以一定要确保...乌尔看结果有...

+0

只是一个建议,但为什么不在界面生成器中创建按钮并将IBAction映射到其touchUpInside事件?可以使工作更轻松。你有没有尝试过使用手势识别器?我猜测容器是第一次接收触摸事件,并没有将它传递给它的子视图,因为它的用户交互标志被禁用了。对不起我没有与我的Mac的权利,但希望这些建议帮助! – shawndreck

回答

7

你没有给任何控制事件按钮从UIControlStateNormal

7

.Change到UIControlEventTouchUpInside而不是使用UIControlStateNormal为ControlEvents使用UIControlEventTouchUpInside这样

[btn addTarget:self action:@selector(_btnTapped) forControlEvents:UIControlEventTouchUpInside]; 
+0

感谢...回复这是问题。我会接受这两个答案,如果我可以,我也投你的答案。 – user1118019

1

试着这样做:

[btn.titleLabel setUserInteractionEnabled: NO];