【问题标题】:How to show a footer view with custom text at the end of the UItableview如何在 UItableview 的末尾显示带有自定义文本的页脚视图
【发布时间】:2016-08-23 04:17:10
【问题描述】:

我有一个 UITableView 来显示搜索结果。用户通过滚动到末尾来加载更多结果。当服务器没有更多结果时,我想在表格视图的末尾显示一个文本,上面写着“没有更多的搜索结果”。我尝试了以下代码,但它不起作用。任何关于正在发生的事情的建议将不胜感激。

UILabel *endLabel = [[UILabel alloc] init];
endLabel.text = @"No more results to display";
[endLabel sizeToFit];
self.tableView.tableFooterView = endLabel;
[self.tableView reloadData];

【问题讨论】:

  • 您不需要重新加载表格来添加表格页脚。确保标签设置正确。验证其框架。尝试设置标签的颜色和字体。
  • @MichaelFourre 那是每个部分的页脚而不是 tableView。
  • @MichaelFourre 如果 OP 有多个部分,它将显示多个页脚并且 OP 不想要。
  • 设置UILabel的Frame来显示它。使用initWithFrame 构造函数而不是init
  • @MichaelFourre 这需要太多的代码和条件,而不是我们可以使用 OP 使用的内置 tableFooterView,这是击球方式。

标签: ios objective-c uitableview cocoa-touch tablefooterview


【解决方案1】:

以下代码可以帮助您解决问题。

您可以将此代码写入您的 viewDidLoad。

UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
        [footer addSubview:yourlabel];
        [footer bringSubviewToFront:yourlabel];
        yourlabel.text =  @"whatever you want to write";
        yourlabel.frame = CGRectMake(self.view.frame.size.width/2 - yourlabel.frame.size.width/2 , 30, yourlabel.frame.size.width, yourlabel.frame.size.height);
        tbl_view.tableFooterView = footer;

但是您需要增加表格页脚视图的大小。

【讨论】:

    【解决方案2】:

    我。在 viewController 的 xib 中创建一个单独的视图。通过单独的视图,我的意思是不要将 viewController 视图中的视图作为子视图拖动,而只需将其拖动到 Interface Builder 中的空白处。

    二。现在设计视图,即在其中放置一个标签,表示不再有数据。

    三。现在在您的 viewController 中创建该视图的出口。

    @property(nonatomic, strong) IBOutlet UIView * tableFooterView;  
    

    四。当没有更多数据时,只需写入

    self.tableView.tableFooterView = self.tableFooterView;  
    

    V。在viewDidLoad中设置页脚的宽度和高度

    self.tableFooterView.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 40);
    

    六。当你确实有数据并且不想显示没有更多数据的东西时

    self.tableView.tableFooterView = nil;
    

    【讨论】:

      【解决方案3】:
      //custom header for footer
      - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
           UILabel *endLabel = [[UILabel alloc] init];
           endLabel.text = @"No more results to display";
          UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.tblView.frame.size.width, 40)];
          [footerView addSubview:endLabel];
          return footerView;
          }
      
      
      //Method To Get Height For Header and Footer
      
      - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
           return 40.0;// your view height
      }
      

      【讨论】:

      • 我在尝试此代码时遇到错误。你的意思是footerView.contentView 你有headerView.contentView 吗?
      • 不需要使用节页脚。 OP 只需要在表格末尾有一个页脚。
      • footerView 没有contentView。为什么footerView 的起源是0, 50
      • 请根据需要设置自定义视图框架。这只是一个例子。
      • 方法被调用了吗?
      猜你喜欢
      • 1970-01-01
      • 2019-12-22
      • 2016-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      相关资源
      最近更新 更多