【问题标题】:UIButton not responding to click event in UITableViewCellUIButton 没有响应 UITableViewCell 中的单击事件
【发布时间】:2012-05-24 06:25:41
【问题描述】:

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

//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中应该很常见,但是没有人有解决方案???

编辑:当单击按钮时,它会在 xcode 控制台中打印 NSLOG msg...所以请确保...您正在查看那里的结果...

【问题讨论】:

  • 只是一个建议,但为什么不在界面构建器中创建按钮并将 IBAction 映射到它的 touchUpInside 事件呢?可以让工作更轻松。您是否也尝试过使用手势识别器?我猜容器首先接收到触摸事件并且它没有将它传递给它的子视图,因为它的用户交互标志可能被禁用了。抱歉,我的 mac 不对,但希望这些建议对您有所帮助!

标签: ios uitableview uibutton touch


【解决方案1】:

你没有给按钮任何控制事件。把它从UIControlStateNormal改成UIControlEventTouchUpInside

【讨论】:

    【解决方案2】:

    不要将UIControlStateNormal 用于ControlEvents,而是像这样使用UIControlEventTouchUpInside

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

    【讨论】:

    • 感谢...回复这是问题所在。如果可以的话,我会接受这两个答案,我也会投票赞成你的答案。
    【解决方案3】:

    尝试这样做:

    [btn.titleLabel setUserInteractionEnabled: NO];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多