Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Edit:

As an alternative solution one can add button by adding extra cell at the end.

like image 498
vivek bhoraniya Avatar asked Dec 03 '13 09:12

vivek bhoraniya


People also ask

How do you add a footer in tableView?

To create a basic header or footer with a text label, override the tableView(_:titleForHeaderInSection:) or tableView(_:titleForFooterInSection:) method of your table's data source object. The table view creates a standard header or footer for you and inserts it into the table at the specified location.

How do you get the Indexpath row when a button in a cell is tapped?

You can walk up the view hierarchy to find the containing table view cell and the containing table view. Then you can ask the table view for the cell's index path. Then use it to find the index path like this: func addButtonTapped(_ sender: Any) { guard let button = sender as?

What is tableView in Swift?

A table view is an instance of the UITableView class, which is a subclass of UIScrollView. Table view cells, which are the repeatable rows, or views, shown in the table view. A table view cell is an instance of a UITableViewCell class, and that class is often subclassed to create custom table view cells.


2 Answers

Per Apple's Docs you must also implement the heightForFooterInSection method, otherwise your viewForFooterInSection wouldn't do anything.

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

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

- (void)addCharity:(id)sender
{
      NSLog(@"add to charity");
}
like image 154
Shankar BS Avatar answered Sep 20 '22 01:09

Shankar BS


Same code in swift

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 100.0
    }
    func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

        let footerView = UIView(frame:CGRectMake(0,0,320,40))

        let loginButton = UIButton(type: .Custom)
        loginButton.setTitle("LOGIN", forState: .Normal)
        loginButton.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
        loginButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        loginButton.backgroundColor = UIColor.blueColor()
        loginButton.frame = CGRectMake(0, 0, 130, 30)

        footerView.addSubview(loginButton)

        return footerView
    }

    func loginAction()
    {
        print("Hello");
    }
like image 45
Avijit Nagare Avatar answered Sep 19 '22 01:09

Avijit Nagare