【问题标题】:Add button in tableview footer在 tableview 页脚中添加按钮
【发布时间】:2013-12-19 07:27:35
【问题描述】:

我在一个视图控制器中有表视图。我有一个部分。我想在页脚中添加按钮。我已经编写了这段代码,但页脚视图没有显示。

- (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个数据。但是当我搜索特定数据并且结果将显示在表格视图中时,我有一个数据,所以当时页脚位置发生了变化。我想在初始位置修复页脚。

编辑:

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

【问题讨论】:

  • 如果表格视图以普通样式(UITableViewStylePlain)创建,则不会调用此方法。检查文档并确保表格视图已分组。
  • 也不适用于团体风格
  • 两种风格都会调用
  • Apple Docs: 这个方法只有在 tableView:heightForFooterInSection: 也实现的情况下才能正常工作。

标签: ios iphone uitableview ios7


【解决方案1】:

根据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)  //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");
}

【讨论】:

  • 是的,这很好用……但我面临的另一个问题是……我想在特定页面修复页脚……它会根据单元格中的数据而变化。有关信息,请参见我的屏幕截图。
  • 你的截图在哪里..?
  • 即搜索结果tableview .. ?在这种情况下,您可以根据您可以发送页脚视图来比较正在显示的表格视图
  • 但我的观点是,当页脚中只有一个数据时,页脚会上升。
【解决方案2】:

swift 中的相同代码

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 100.0
    }
    func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

        let footerView = UIView(frame:CGRectMake(0,0,320,40))

        let loginButton = UIButton(type: .Custom)
        loginButton.setTitle("LOGIN", forState: .Normal)
        loginButton.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
        loginButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        loginButton.backgroundColor = UIColor.blueColor()
        loginButton.frame = CGRectMake(0, 0, 130, 30)

        footerView.addSubview(loginButton)

        return footerView
    }

    func loginAction()
    {
        print("Hello");
    }

【讨论】:

    【解决方案3】:

    确保三件事

    1- 你没有实现这个方法

    - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
    

    2- 这个方法

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

    是UITableViewDelegate方法而不是UITableViewDataSource,检查tableview的delegate是否用控制器设置

    3- 确保您正在实施此方法

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
    

    最后,如果一切正常但仍然无法正常工作,那么您只需将表格的“tableFooterView”属性中的视图添加为整个表格的页脚,而不仅仅是部分

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      相关资源
      最近更新 更多