ios – 在表格页脚中添加按钮

我在一个viewcontroller中有tableview.我有一个部分.我想在页脚中添加按钮.我写了这个代码,但页脚视图没有显示.
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(0,320,40)];
    UIButton *addcharity=[UIButton buttonWithType:UIButtonTypeCustom];
    [addcharity setTitle:@"Add to other" forState:UIControlStateNormal];
    [addcharity addTarget:self action:@selector(addCharity:) forControlEvents:UIControlEventTouchUpInside];
    addcharity.frame=CGRectMake(0,40,30);
    [footerView addSubview:addcharity];
    return footerView;

}

我也在其代表方法中设置页脚100的高度.

截图:
最初我有3个数据.但是当我搜索特定的数据和结果将显示在tableview当时我有一个数据,所以当时脚注位置改变.我想在最初的地方修复页脚.

解决方法

每 Apple’s Docs你还必须实现heightForFooterInSection方法,否则你的ViewForFooterInSection不会做任何事情.
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
      return 100.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if(tableView == myListTableview) //hear u can make decision 
    {
       UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(0,40)];
       UIButton *addcharity=[UIButton buttonWithType:UIButtonTypeCustom];
       [addcharity setTitle:@"Add to other" forState:UIControlStateNormal];
       [addcharity addTarget:self action:@selector(addCharity:) forControlEvents:UIControlEventTouchUpInside];
       [addcharity setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//set the color this is may be different for iOS 7
       addcharity.frame=CGRectMake(0,130,30); //set some large width to ur title 
       [footerView addSubview:addcharity];
       return footerView;
    }
}

- (void)addCharity:(id)sender
{
      NSLog(@"add to charity");
}

以上是来客网为你收集整理的ios – 在表格页脚中添加按钮全部内容,希望文章能够帮你解决ios – 在表格页脚中添加按钮所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。