UITableView footerView带按钮,按钮不起作用

时间:2010-10-21 16:31:44

标签: iphone uitableview footer

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

- (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.");
}

5 个答案:

答案 0 :(得分:38)

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

答案 1 :(得分:7)

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 50.0f;
}

把这个方法放在你的课堂上你的问题会解决.....

答案 2 :(得分:1)

它为我工作我只需添加

footerView.frame =CGRectMake(0, 300, 100, 100);

答案 3 :(得分:0)

在调用 viewForFooterInSection 之前,您需要设置tableView的 sectionFooterHeight 属性(无法在viewForFooterInSection方法中执行此操作)。

答案 4 :(得分:-8)

创建UIView的子类并包含以下方法:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    return [super hitTest:point withEvent:event];
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    return [super pointInside:point withEvent:event];
}