Add button in tableview footer

后端 未结 3 1455
轻奢々
轻奢々 2021-02-19 07:18

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.

-         


        
相关标签:
3条回答
  • 2021-02-19 07:40

    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");
    }
    
    0 讨论(0)
  • 2021-02-19 07:42

    Be sure of three things

    1- You not implementing this method

    - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
    

    2- This method

    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    

    Is a UITableViewDelegate method and not UITableViewDataSource, Check if delegate of the tableview is set with the controller

    3- Be sure you are implementing this method

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
    

    Finally, If everything is correct and still not working then You can just add the view in "tableFooterView" property of the table to be the footer of the whole table and not only the section

    0 讨论(0)
  • 2021-02-19 08:00

    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");
        }
    
    0 讨论(0)
提交回复
热议问题