带按钮的UITableView footerView,按钮不起作用 [英] UITableView footerView with button, button doesn't work

查看:108
本文介绍了带按钮的UITableView footerView,按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的UITableView页脚视图创建一个自定义视图,其中包含一个按钮。我可以在那里看到它,但当我触摸它时,没有任何反应......你能看出这里有什么问题:

I'm trying to create a custom view for my UITableView footerView that has a button in it. I can see it there, but when I touch it, nothing happens... can you see what's wrong here:

- (void)viewDidLoad {
    [super viewDidLoad];

    if(self.tableView.tableFooterView == nil) {
        //allocate the view if it doesn't exist yet
        UIView *footerView  = [[UIView alloc] init];

        //create the button
        UIButton *addNewBtn = [UIButton buttonWithType:UIButtonTypeCustom];     
        //the button should be as big as a table view cell
        [addNewBtn setFrame:CGRectMake(0, 0, 320, [AddMerchantTVC cellHeight])];

        //set title, font size and font color
        [addNewBtn setTitle:@"Add New" forState:UIControlStateNormal];
        [addNewBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
        [addNewBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [addNewBtn addTarget:self action:@selector(addNew:) forControlEvents:UIControlEventTouchUpInside];

        UIImageView *cellGradient = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellGradient.png"]];
        cellGradient.frame = CGRectMake(0, 54, 320, 16);
        [footerView addSubview:cellGradient];

        //add the button to the view
        [footerView addSubview:addNewBtn];
        footerView.userInteractionEnabled = YES;
        self.tableView.tableFooterView = footerView;
        self.tableView.tableFooterView.userInteractionEnabled = YES;

        [footerView release];
        [cellGradient release];
}

- (void)addNew:(id)sender {
    // This is never called
    NSLog(@"Touched.");
}


推荐答案

你没有设置footerView的框架。将footerView的框架设置为至少与按钮一样高,否则触摸不会传递到按钮。

You're not setting the footerView's frame. Set the frame of the footerView to be at least as high as the button otherwise the touches don't get passed down to the button.

这篇关于带按钮的UITableView footerView,按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆