向viewForFooterSection UITableView添加按钮

Mar*_*cal 0 iphone uitableview uiscrollview ios

在讨论了这个主题之后,我正在努力在UItableView上的footerview上实现2个按钮.要创建按钮,我在声明.h文件中声明它们,在实现.m文件上合成然后,我通过代码创建它们,如下所示:

- (UIButton *)resetButton{
if (resetButton == nil)
 {

 resetButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
 resetButton.frame = CGRectMake(20.0, 40 , 95.0, 37.0);
 [resetButton setTitle:@"Reset" forState:UIControlStateNormal];
 resetButton.backgroundColor = [UIColor clearColor];
 [resetButton addTarget:self action:@selector(resetAction:) forControlEvents:UIControlEventTouchDown];

    resetButton.tag = 1;    
 }
 return resetButton;
 }
Run Code Online (Sandbox Code Playgroud)

然后,我将此代码实现到UITableView委托方法中:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{

    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 100.0)];

        [customView addSubview:self.resetButton];
    [customView addSubview:self.calculateButton];

    return [customView autorelease];          
}
Run Code Online (Sandbox Code Playgroud)

通过这样做,按钮出现在屏幕上,但是当我点击它们时,没有任何反应(我在动作上实现了AlertView以检查它们是否有效.

这有什么帮助吗?

谢谢!

编辑:链接到按钮的操作:

-(IBAction)resetAction:(id)sender {

    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Reset"
                                                 message:@"You just pressed the Reset button"
                                                delegate:self
                                       cancelButtonTitle:@"Acknowledged"
                                       otherButtonTitles:nil];
    [alert show];
    [alert release];

}
Run Code Online (Sandbox Code Playgroud)

Pat*_*dez 5

我想你缺少的是- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section.

如果没有这个,自定义页脚将为高度0.您仍然可以看到该按钮的原因clipsToBoundsNO因为默认情况下是自定义视图.