简体   繁体   English

如何修复tableview底部的按钮?

[英]How to fix a button at the bottom of the tableview?

In my App i want to fix a button at the bottom of the table view.在我的应用程序中,我想修复表格视图底部的按钮。

Here is my Initial Screen,这是我的初始屏幕,

在此处输入图片说明

Button created at the footer section在页脚部分创建的按钮

-(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;
}

After that i am getting like this,之后我就变成这样了

在此处输入图片说明

How can i fix that?我该如何解决?

why you dont make something like this?你为什么不做这样的事情?

-(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;

}

and call this after you init the table并在初始化表后调用它

-(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;

}

Put above method in viewDidLoad() Method and after initializing tableview将上述方法放在 viewDidLoad() 方法中并在初始化 tableview 之后

If you want section footer/header scroll with tableview, you need to use a grouped style table view.如果您想使用 tableview 滚动部分页脚/页眉,则需要使用分组样式的 table view。

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

And a grouped style table view will have a default backgroundColor and section header/footer height.分组样式表视图将具有默认的 backgroundColor 和节页眉/页脚高度。 You need set these value explicitly.您需要明确设置这些值。

If you have only one section, it is better to use tableFooterView instead of section footer view如果只有一节,最好使用tableFooterView而不是节页脚视图

While Scrolling that button visible means ,UITableview cell are reusable so that button is available ,what you have to do means .虽然滚动该按钮可见意味着,UITableview 单元格是可重用的,以便该按钮可用,您必须做的事情意味着。

1.Take number of row in UITableView , 1.在 UITableView 中取行数,

Then inside cell for row at indexpath :然后在 indexpath 行的单元格内:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM