How to add a footer to a UITableView?

I use this code to add a footer to a TableView. It has 20 sections, and each section consists of several lines. There is a method titleForHeaderInSection and sectionForSectionIndexTitle.

CGRect footerRect = CGRectMake(0, 0, 320, 40); UILabel *tableFooter = [[UILabel alloc] initWithFrame:footerRect]; tableFooter.textColor = [UIColor blueColor]; tableFooter.backgroundColor = [self.theTable backgroundColor]; tableFooter.opaque = YES; tableFooter.font = [UIFont boldSystemFontOfSize:15]; tableFooter.text = @"test"; self.theTable.tableFooterView = tableFooter; [tableFooter release]; 

What am I doing wrong?

thank,

RL

+51
iphone uitableview uiview footer
Feb 28 2018-11-18T00:
source share
11 answers

I see code in my code that

 self.theTable.tableFooterView = tableFooter; 

works and

 [self.theTable.tableFooterView addSubview:tableFooter]; 

does not work. So stick to the former and look elsewhere for a possible mistake. NTN

+35
May 31 '11 at 19:13
source share

You need to implement the UITableViewDelegate method

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

and return the desired view (for example, UILabel with the text you need in the footer) for the corresponding section of the table.

+35
Jun 06 2018-12-06T00:
source share

I used this and it worked great :)

  UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]; [footerView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"ProductCellBackground.png"]]]; self.tableView.tableFooterView = footerView; [self.tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)]; [self.tableView setContentInset:(UIEdgeInsetsMake(0, 0, -500, 0))]; 
+18
Jun 28 2018-12-12T00:
source share

I know this is a pretty old question, but I just met the same problem. I don’t know exactly why, but it seems that tableFooterView can only be an instance of UIView (not β€œview” but β€œis a member”) ... Therefore, in my case, I created a new UIView object (like wrapperView) and add my own subordinate to it ... In your case, create code from:

 CGRect footerRect = CGRectMake(0, 0, 320, 40); UILabel *tableFooter = [[UILabel alloc] initWithFrame:footerRect]; tableFooter.textColor = [UIColor blueColor]; tableFooter.backgroundColor = [self.theTable backgroundColor]; tableFooter.opaque = YES; tableFooter.font = [UIFont boldSystemFontOfSize:15]; tableFooter.text = @"test"; self.theTable.tableFooterView = tableFooter; [tableFooter release]; 

at

 CGRect footerRect = CGRectMake(0, 0, 320, 40); UIView *wrapperView = [[UIView alloc] initWithFrame:footerRect]; UILabel *tableFooter = [[UILabel alloc] initWithFrame:footerRect]; tableFooter.textColor = [UIColor blueColor]; tableFooter.backgroundColor = [self.theTable backgroundColor]; tableFooter.opaque = YES; tableFooter.font = [UIFont boldSystemFontOfSize:15]; tableFooter.text = @"test"; [wrapperView addSubview:tableFooter]; self.theTable.tableFooterView = wrapperView; [wrapperView release]; [tableFooter release]; 

Hope this helps. This works for me.

+6
Sep 20 '13 at 7:44
source share

At first, I was just trying to use a method:

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

but after using this together with:

 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section 
The problem was solved. Example Program -
 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 30.0f; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *sampleView = [[UIView alloc] init]; sampleView.frame = CGRectMake(SCREEN_WIDTH/2, 5, 60, 4); sampleView.backgroundColor = [UIColor blackColor]; return sampleView; } 

and enable the UITableViewDelegate protocol.

 @interface TestViewController : UIViewController <UITableViewDelegate> 
+6
Dec 21 '14 at 14:45
source share

Swift 2.1.1 below works:

 func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { let v = UIView() v.backgroundColor = UIColor.RGB(53, 60, 62) return v } func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return 80 } 

If self.theTable.tableFooterView = tableFooter , there is a space between the last row and tableFooterView .

+4
Feb 14 '16 at 9:18
source share
 [self.tableView setTableFooterView:footerView]; 
+2
Apr 12 '17 at 6:07 on
source share

instead

 self.theTable.tableFooterView = tableFooter; 

try

 [self.theTable.tableFooterView addSubview:tableFooter]; 
+1
Feb 28 2018-11-18T00:
source share

I had the same problem, but I replaced the following line in my header:

 @interface MyController : UIViewTableViewController <UITableViewDelegate, UITableViewDataSource> 

with this line and works as expected:

 @interface RequestViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 

Pay attention to the UIViewController. Good luck :)

+1
Sep 04 2018-11-11T00:
source share

These samples work well. You can check the section and then return the height to show or hide the section. Remember to extend your viewcontroller from a UITableViewDelegate .

Objective-c

 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { if (section == 0) { // to hide footer for section 0 return 0.0; } else { // show footer for every section except section 0 return HEIGHT_YOU_WANT; } } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *footerView = [[UIView alloc] init]; footerView.backgroundColor = [UIColor blackColor]; return footerView; } 

swift

 func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { let footerView = UIView() footerView.backgroundColor = UIColor.black return footerView } func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { if section == 0 { // to hide footer for section 0 return 0.0 } else { // show footer for every section except section 0 return HEIGHT_YOU_WANT } } 
+1
Mar 21 '18 at 8:26
source share

If you do not prefer the effect of a sticky bottom, I would put it in viewDidLoad() https://stackoverflow.com/a/416829/

0
Jul 04 '16 at 3:18
source share



All Articles