2010-10-21 199 views
23

我想爲我的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."); 
} 

回答

36

你」不要設置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

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

我真的需要的UIView的子類來實現這一點?如果是這樣,那很煩人。 – 2010-10-21 16:58:33

0

你需要調用之前設置的tableView的sectionFooterHeight屬性viewForFooterInSection時(不能做到這一點的方法viewForFooterInSection內)。

+0

特別是如果你使用XIB構建你的UIView,你需要實現這個功能 – 2012-06-04 07:44:38

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

把這種方法在你的類的問題將解決.....

1

它的工作對我來說,我只是添加

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