UITableView footerView按钮,按钮不起作用

问题描述:

我想为我的UITableView footerView创建一个自定义视图,该按钮中有一个按钮。我可以在那里看到它,但是当我触摸它,什么也没有发生......你可以看到什么是错在这里:UITableView footerView按钮,按钮不起作用

- (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的框架设置为至少与按钮一样高,否则触摸不会传递到按钮。

+0

就是这样。谢谢! – 2010-10-21 17:58:46

+1

这对我来说当我实现' - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)节; '指定页脚高度。 – 2013-06-09 10:56:22

+0

这也适用于我,谢谢! – 2017-08-23 18:38:20

创建的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]; 
} 
+0

我真的需要的UIView的子类来实现这一点?如果是这样,那很烦人。 – 2010-10-21 16:58:33

你需要调用之前设置的tableView的sectionFooterHeight属性viewForFooterInSection时(不能做到这一点的方法viewForFooterInSection内)。

+0

特别是如果你使用XIB构建你的UIView,你需要实现这个功能 – 2012-06-04 07:44:38

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

把这种方法在你的类的问题将解决.....

它的工作对我来说,我只是添加

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