繁体   English   中英

UITut中的UIButton在UITableView =中无法单击该按钮

[英]UIButton in UIView in UITableView = can't click the button

如果我直接添加按钮一切都很顺利。 我真的想将它添加到视图中并仍然可以单击它。 这是代码:

UIView *containerView =
[[[UIView alloc]
initWithFrame:CGRectMake(0, 0, 300, 60)]
autorelease];
UIButton *footerButton = [UIButton buttonWithType:UIButtonTypeCustom];
footerButton.frame = CGRectMake(10, 10, 300, 40);
[footerButton addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[containerView addSubview:footerButton];

您是否将页脚高度设置为要添加到其中的视图的高度? 当我在方法viewForFooterInSection中返回包含UIButton的UIView时,我遇到了类似的问题。 在更新heightForFooterInSection方法之前,页脚高度不够大。 例如:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return containerView.bounds.size.height;
}

这个@selector(click)看起来有点奇怪。 我原以为你的点击功能会像:

- (void)click:(id)sender;

如果这就是你所拥有的那么你需要添加冒号@selector(click:)以使签名匹配。

一切正常! 在你的TableViewController中执行:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)] autorelease];
    UIButton *footerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    footerButton.frame = CGRectMake(10, 10, 300, 40);
    [footerButton addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [containerView addSubview:footerButton];

    [footerButton setTitle:@"Touch me!" forState:UIControlStateNormal];
    containerView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    self.tableView.tableFooterView = containerView;
}

- (void)click {
    NSLog(@"I am here!");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM