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.
Edit:
As an alternative solution one can add button by adding extra cell at the end.
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.
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?
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.
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");
}
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With