2

In my App i want to fix a button at the bottom of the table view.

Here is my Initial Screen,

enter image description here

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,

enter image description here

How can i fix that?

User558
  • 1,165
  • 1
  • 13
  • 37

4 Answers4

3

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

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

Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65
  • addEdu.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; addEdu.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; remove above code then try......... – Hitesh Surani Apr 21 '16 at 12:34
  • Hello dear, Simply try this set image only to your footer view then let me know whats happen's.Please refer my above edited answer – Hitesh Surani Apr 21 '16 at 12:44
  • it is showing image over the tableview.not fixed at bottom – User558 Apr 21 '16 at 13:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109873/discussion-between-user558-and-hitesh-surani). – User558 Apr 22 '16 at 05:56
0

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

Jeff
  • 166
  • 5
0

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;
}
Kishore Kumar
  • 4,265
  • 3
  • 26
  • 47