gpt4 book ai didi

ios - UIButton 不响应 UITableViewCell 中的单击事件

转载 作者:可可西里 更新时间:2023-11-01 04:11:35 28 4
gpt4 key购买 nike

iOS 中有一个黑暗魔法阻止我的按钮被点击。如果我不向 uitableviewcell 添加按钮,然后单击该按钮,则会触发该事件。但如果按钮在 uitableviewcell 中,它不会被触发,看起来是 table我已经准备好示例代码,如果你们能帮助我,请在 xcode 中创建一个简单的单 View 应用程序,然后粘贴以下代码

//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 消息...因此请确保...您在那里查看结果...

最佳答案

您还没有给按钮任何控制事件。将它从 UIControlStateNormal 更改为 UIControlEventTouchUpInside

关于ios - UIButton 不响应 UITableViewCell 中的单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10732229/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com