After add a footerview to the tableview, if the table has one cell only , the footer displays a little high just at the bottom of the cell. How to make it display at the foot of the tableview no matter how many cells the table has?
-
the tablefooterview's location is depend on how many section/cell and your cell's height,if you want the footerview always at the foot of the tableview, i think only custom view can help you. – Mistrx丶 Mar 03 '17 at 01:50
-
that does not work. if i add a custom view to the tableview at the same time there are many cells in the table ,and the customview will never display below the last cell. – Terminater Mar 03 '17 at 02:14
3 Answers
footerView is just the background, add a view that you really want, just like this:
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView.init()
footerView.backgroundColor = .red
// footerView is just the background, add a view that you really want
let subView = UIView.init(frame: CGRect.init(x: 20, y: 20, width: UIScreen.main.bounds.width - 40, height: 100 - 40))
subView.backgroundColor = .yellow
footerView.addSubview(subView)
return footerView
}

- 51
- 3
-
The question is tagged Objective-C. Pleas post answers in the appropriate language. – rmaddy Mar 03 '17 at 03:12
-
i return a label in this way , does not work . the footer displays always below the last cell . if the first cell is also the last , the footer displays a little high. add subviews to footer can not change its position. – Terminater Mar 03 '17 at 03:34
-
return a view which has a label. you can't change the 'return view's frame, but you can change the label's position – Mr Fox Mar 03 '17 at 03:42
Here is my code to set view on tableFooterView and its working fine.
@property (weak, nonatomic) IBOutlet UITableView *tblView;
@property (weak, nonatomic) IBOutlet UIView *viewTableFooterView;
- (void)viewDidLoad {
[super viewDidLoad];
_tblView.tableFooterView = _viewTableFooterView;
_tblView.backgroundColor = [UIColor greenColor];
[_tblView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
return cell;
}
Check code and let me know if its not working for you.

- 5,079
- 4
- 26
- 56
This occurs because the footer view that you are adding is the footer of the content you are displaying inside the UITableView
. That being said there is a way to get the output you want. What you can do is drag a UIView
below the UITableViewCell
added in the storyboard for it to appear as a footer.
Setup constraints for the UILabel
or anything you want to show inside with respect to this view, what I mean is don't add fixed height constraints otherwise you will get annoying layout breaking warnings.
Make a duplicate of this footer view and add it to the bottom your UITableView
and add a fixed height for it to make it as a copy of the footer view you added inside the UITableView
. Sort of like this:
Now, create an outlet for this height constraint of the footer view into your UIViewController
.
Now in your viewDidLoad
what you can do is check if the height of the UITableView
after adding the cells is less than screen bounds and proceed accordingly, like this:
- (void)viewDidLoad {
[super viewDidLoad];
//dont show footer view if cells are within screen bounds
if (numberOfCells * cellHeight < [UIScreen mainScreen].bounds.size.height){
UIView *footerView = mainTableView.tableFooterView;
CGRect tempFrame = footerView.frame;
tempFrame.size.height = 0.0f;
footerView.frame = tempFrame;
mainTableView.tableFooterView = footerView;
}else{
//dont show footerview below table view if cells are outside screen bounds
[footerHeightConstraint setConstant:0.0f];
}
}
Note: I have missed an edge case related to height like the product of rows * height coming to be equal or just less than screen bounds, so you'll have to have a look at that.

- 4,078
- 3
- 15
- 35
-
good idea.But that just means UITablefooterview can not accomplish this ? – Terminater Mar 03 '17 at 06:18
-
As far as i know, nope. Whatever behaviour you see is the default behaviour. – Rikh Mar 03 '17 at 06:20