vivek vivek - 5 months ago 54
iOS Question

Add button in tableview footer

I have tableview in one viewcontroller. I have one section in that. I want to add button in footer. I have written this code but footer view is not displaying.

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

}


I have also set height of footer 100 in its delegate method.

screenshots:
Initially i have 3 data. But when I search for particular data and result will be displayed in tableview at that time I have one data so at that time footer location changed. I want to to fix footer at initial place.
enter image description here

enter image description here

Answer

I think what u are doing is correct

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
      return 100.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if(tableView == myListTableview) //hear u 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 to ur title 
       [footerView addSubview:addcharity];
       return footerView;
    }
}

- (void)addCharity:(id)sender
{
      NSLog(@"add to charity");
}

hope this helps u .. :)

Comments