简体   繁体   中英

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

If you want section footer/header scroll with tableview, you need to use a grouped style table view.

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

And a grouped style table view will have a default backgroundColor and section header/footer height. You need set these value explicitly.

If you have only one section, it is better to use tableFooterView instead of section footer view

While Scrolling that button visible means ,UITableview cell are reusable so that button is available ,what you have to do means .

1.Take number of row in UITableView ,

Then inside cell for row at indexpath :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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