在tableview页脚中添加按钮

 走ln方 发布于 2023-02-13 11:43

我在一个viewcontroller中有tableview.我有一节.我想在页脚中添加按钮.我写了这段代码,但页脚视图没有显示.

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

    UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 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, 0, 40, 30);
    [footerView addSubview:addcharity];
    return footerView;

}

我还在其委托方法中设置了页脚100的高度.

截图:最初我有3个数据.但是当我搜索特定数据并且结果将在tableview中显示时,我有一个数据,所以那时页脚位置发生了变化.我想在最初的地方修剪页脚. 在此输入图像描述

在此输入图像描述

编辑:

作为替代解决方案,可以通过在末尾添加额外的单元来添加按钮.

1 个回答
  • 根据Apple的Docs,您还必须实施该heightForFooterInSection方法,否则您viewForFooterInSection不会做任何事情.

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    {
          return 100.0f;
    }
    
    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
    {
        if(tableView == myListTableview)  //Here you can make decision 
        {
           UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 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, 0, 130, 30); //Set some large width for your title 
           [footerView addSubview:addcharity];
           return footerView;
        }
    }
    
    - (void)addCharity:(id)sender
    {
          NSLog(@"add to charity");
    }
    

    2023-02-13 11:45 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有