【问题标题】:How to Add footer text only in text last section of the tableview?如何仅在表格视图的文本最后一部分中添加页脚文本?
【发布时间】:2014-03-28 09:22:22
【问题描述】:

我只想在TableView 的最后部分显示页脚文本。在TableView 我有很多来自API 的TableView 部分。但我只需要在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 部分的底部。

【问题讨论】:

  • Tableview 本身添加页脚怎么样。
  • 我需要在最后一段显示一些文字
  • 我正在使用Section tableview,您能告诉我如何在tableView 本身中实现吗?
  • @Rey_mysterio 如果你想要页脚视图到 tableview,请查看我的答案。

标签: ios ios5 tableview uitableview


【解决方案1】:

试试这个

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

【讨论】:

    【解决方案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;
    

    【讨论】:

    • 感谢您的回答..知道了
    • 到底为什么不固定在底部?
    • 我需要尽可能修复底部的视图
    • 哦,伙计,很高兴你得到了答案。
    • 如何固定底部的页脚?
    【解决方案3】:

    试试这个,

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

    【讨论】:

    • 究竟为什么不固定在底部
    • 我需要尽可能修复底部的视图
    【解决方案4】:

    如果你想要tableview的footerview,请尝试这样使用。

    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;
    

    【讨论】:

      猜你喜欢
      • 2018-10-23
      • 2018-01-04
      • 2013-05-07
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多