【问题标题】:How to fix a button at the bottom of the tableview?如何修复表格视图底部的按钮?
【发布时间】:2016-04-21 12:02:20
【问题描述】:

在我的应用程序中,我想在表格视图底部修复一个按钮。

这是我的初始屏幕,

在页脚部分创建的按钮

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if(tableView == educateTbl) 
{
    float footerWidth = 150.0f;
    float padding = 10.0f;
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, footerWidth, 50.0)];
    footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    UIButton *addEdu = [[UIButton alloc]initWithFrame:CGRectMake(padding, 0, footerWidth - 2.0f * padding, 44.0f)];
    addEdu.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    addEdu.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

    [addEdu setTitle:@"Add Education" forState:UIControlStateNormal];
    [addEdu addTarget:self action:@selector(addEducation:) forControlEvents:UIControlEventTouchUpInside];
    [addEdu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    addEdu.frame=CGRectMake(0, 0, 200, 30); 
    [footerView addSubview:addEdu];
    return footerView;
}
return nil;
}

后来我就这样了,

我该如何解决这个问题?

【问题讨论】:

  • 在按钮中添加约束
  • 我建议创建一个视图并将其设置为tableView.tableFooterView
  • 以上代码将为特定部分设置页脚视图。尝试像这样将视图设置为 tableFooterView,self.tableView.tableFooterView = addEdu;
  • 你也实现了-tableView:heightForFooterInSection:吗?
  • 为页脚视图设置不同的背景颜色。这样您就可以了解框架大小及其放置位置

标签: ios objective-c uitableview uibutton tablefooterview


【解决方案1】:

你为什么不做这样的东西?

-(void)setTableFooter
{
   float footerWidth = 150.0f;
   float padding = 10.0f;
   UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, footerWidth, 50.0)];
   footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

   UIButton *addEdu = [[UIButton alloc]initWithFrame:CGRectMake(padding, 0, footerWidth - 2.0f * padding, 44.0f)];
   addEdu.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
   addEdu.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

   [addEdu setTitle:@"Add Education" forState:UIControlStateNormal];
   [addEdu addTarget:self action:@selector(addEducation:) forControlEvents:UIControlEventTouchUpInside];
   [addEdu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
addEdu.frame=CGRectMake(0, 0, 200, 30); 
   [footerView addSubview:addEdu];

   tableView.tableFooterView = footerView;

}

在你初始化表格后调用它

【讨论】:

    【解决方案2】:

    如果您希望使用 tableview 滚动部分页脚/页眉,则需要使用分组样式的 table view。

    [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped]

    并且分组样式表视图将具有默认背景颜色和节页眉/页脚高度。您需要明确设置这些值。

    如果你只有一个section,最好使用tableFooterView而不是section footer view

    【讨论】:

      【解决方案3】:

      虽然滚动按钮可见意味着,UITableview 单元格是可重复使用的,因此按钮可用,你必须做的意味着。

      1.取 UITableView 中的行数,

      然后在 indexpath 处的单元格内:

      Total number of row ==indexpath.row
      {
         //inside this button.hidden=no;
      }
      else
      {
         //button.hidden=yes;
      }
      

      【讨论】:

        【解决方案4】:
        -(void)CreateCustomFooter
        {
           float footerWidth = 150.0f;
           float padding = 10.0f;
           UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, footerWidth, 50.0)];
           footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        
           UIButton *addEdu = [[UIButton alloc]initWithFrame:CGRectMake(padding, 0, footerWidth - 2.0f * padding, 44.0f)];
           addEdu.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
           addEdu.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        
           [addEdu setTitle:@"Add Education" forState:UIControlStateNormal];
           [addEdu addTarget:self action:@selector(addEducation:) forControlEvents:UIControlEventTouchUpInside];
           [addEdu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        addEdu.frame=CGRectMake(0, 0, 200, 30); 
           [footerView addSubview:addEdu];
        
           tableView.tableFooterView = footerView;
        
        }
        

        将上面的方法放在viewDidLoad()方法中,并在初始化tableview之后

        【讨论】:

        • addEdu.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; addEdu.contentHorizo​​ntalAlignment = UIControlContentHorizo​​ntalAlignmentCenter;删除上面的代码然后尝试.........
        • 您好,亲爱的,只需在您的页脚视图中尝试此设置图像,然后让我知道发生了什么。请参考我上面编辑的答案
        • 它在 tableview 上显示图像。未固定在底部
        猜你喜欢
        • 2014-02-23
        • 1970-01-01
        • 2021-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-21
        • 2018-07-29
        • 2023-03-30
        相关资源
        最近更新 更多