如何仅在tableview的文本最后一部分中添加页脚文本?

时间:2014-03-28 09:22:22

标签: ios ios5 tableview uitableview

我只想在TableView的最后一部分显示页脚文字。在TableView我有很多TableView部分,它来自API。但我需要显示页脚消息只在tableView部分的底部怎么做?

 (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;
}

目前我使用此代码。这在所有部分底部显示。我只需要在最后一个tableview部分的底部显示。

4 个答案:

答案 0 :(得分:3)

试试这个

- (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;
}
}

答案 1 :(得分:2)

使用

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;

答案 2 :(得分:2)

试试这个,

- (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;
      }
}

或者您可以将视图设置为页脚视图

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

....

[self.tableView setTableFooterView:footer]; 

答案 3 :(得分:1)

如果你想查看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;