UIButton 不响应 UITableViewCell 中的点击事件

编程入门 行业动态 更新时间:2024-10-28 18:26:22
本文介绍了UIButton 不响应 UITableViewCell 中的点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

ios 中的这个黑暗魔法阻止了我的按钮被点击.如果我不向 uitableviewcell 添加按钮,然后单击该按钮,则会触发该事件.但是如果按钮在 uitableviewcell 中,它不会被触发,它似乎是表格我已经准备好了示例代码,如果你们能帮助我,请在 xcode 中创建一个简单的单视图应用程序,然后粘贴以下代码

there is this dark sorcery in ios is preventing my button being clicked. if i don't add button to the uitableviewcell, and i click the button, the event is triggered. but if the button is in uitableviewcell, it won't get triggered, it seems table i have sample code ready, if you guys can help me, please just create a simple single-view application in xcode, and just paste the following code

//GRDViewController.h

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

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

please help i feel this case should be common in ios, but nobody has solution for this???

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

edit: when click on the button it prints NSLOG msg in the xcode console...so make sure...u r looking at the results there...

推荐答案

you have not given any control event to button .Change it to UIControlEventTouchUpInside from UIControlStateNormal

you have not given any control event to button .Change it to UIControlEventTouchUpInside from UIControlStateNormal

这篇关于UIButton 不响应 UITableViewCell 中的点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 02:21:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1388517.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:事件   UIButton   UITableViewCell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!