Question

I want to display footer text only at last section of the TableView.In TableView I have a lot of TableView section it come from API.But I need to display footer message only at bottom of the tableView section how to do it?

 (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 10)];
footer.backgroundColor = [UIColor clearColor];

UILabel *lbl = [[UILabel alloc]initWithFrame:footer.frame];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = @"Your Text";
lbl.textAlignment = NSTextAlignmentCenter;
[footer addSubview:lbl];

return footer;
}


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

Currently Im used this code.This shows in all section bottom.I need to display only at bottom of the last tableview section.

Was it helpful?

Solution

try this

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 55)];
if (section == array.count -1){
    footer.backgroundColor=[UIColor clearColor];
    lbl = [[UILabel alloc]initWithFrame:CGRectMake(0,0,540,40)];
    lbl.backgroundColor = [UIColor clearColor];
    lbl.text = @" Please add your message";
    lbl.textAlignment = NSTextAlignmentCenter;
    [lbl setNumberOfLines:10];//set line if you need
    [lbl setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:14.0]];//font size and style
    [lbl setTextColor:[UIColor blackColor]];
    [footer addSubview:lbl];
    self.jobsTableView.tableFooterView=footer;

}
    return footer;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
if (section == [tableView numberOfSections] - 1) {
    return 60.0;
} else {
    return 0.0;
}
}

OTHER TIPS

use

if (section == yourArray.count -1)
{
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 10)];
footer.backgroundColor = [UIColor clearColor];

UILabel *lbl = [[UILabel alloc]initWithFrame:footer.frame];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = @"Your Text";
lbl.textAlignment = NSTextAlignmentCenter;
[footer addSubview:lbl];
}
return footer;

Try this,

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section 
{
    if (section == [tableView numberOfSections] - 1) {
        return 10.0;
    } else {
        return 0.0;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section             {
      if (section == [tableView numberOfSections] - 1) {

        UIView *footer = ..
        ....
        return footer;
      } else {
        return nil;
      }
}

or you can set view as footer to tableview

UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 10)];

....

[self.tableView setTableFooterView:footer]; 

Please try to use like this if you wanna footerview of tableview.

UIView *footer  = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 10)];
footer.backgroundColor = [UIColor clearColor];

UILabel *lbl = [[UILabel alloc]initWithFrame:footer.frame];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = @"Your Text";
lbl.textAlignment = NSTextAlignmentCenter;
[footer addSubview:lbl];
footer.backgroundColor = [UIColor blackColor];
self.tableView.tableFooterView = footer;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top