2010-10-15 39 views
1

如果我直接添加按鈕,一切都很順利。我真的很想將它添加到視圖中,仍然可以點擊它。這裏的代碼:在UITableView UIView中的UIButton =無法點擊按鈕

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

如何使用'containerView'?你做'[tableView addSubview:containerView]',或者在表格視圖單元格中使用它?您應該嘗試使用UIButtonTypeRoundedRect並查看它是否正在進行交互。 – tia 2010-10-15 03:27:33

+0

我在頁腳中使用 – gurghet 2010-10-15 12:56:30

回答

1

一切正常!在您的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!"); 
} 
+0

謝謝,但不幸的是我在footerviewforsection:method中加載它 – gurghet 2010-10-15 12:58:13

1

@selector(click)看起來有點奇怪。我還以爲你點擊功能想是這樣的:

- (void)click:(id)sender; 

如果這是你所擁有的,那麼你需要添加結腸@selector(click:)使簽名匹配。

+0

我剛更改了代碼以使其簡短,顯然不是那樣 – gurghet 2010-10-15 12:55:32

2

您是否將頁腳高度設置爲要添加到視圖的高度?當我在方法viewForFooterInSection中返回包含UIButton的UIView時,我遇到了類似的問題。在更新heightForFooterInSection方法之前,頁腳高度不夠大。例如:

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