我希望让我的页脚 float 在表格 View 上,以便它始终可见,而不仅仅是在我滚动到底部时。

我尝试使用以下代码调整表 View 的大小:

- (id)initWithStyle:(UITableViewStyle)style
{
    if ((self = [super initWithStyle: style]))
    {
        UIView *footer = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 1, 45)];
        footer.backgroundColor = [UIColor clearColor];
        self.tableView.tableFooterView = footer;
        self.tableView.frame = CGRectMake(0, 0, 320, 100);
        footer.hidden = NO;

    }

到目前为止,我没有运气。

我也尝试过使用其他窗口并调整所有 .XIB 文件的大小。

最佳答案

UITableViewtableFooterView 属性是一个 UIView 对象,它始终显示在内容的底部,在最后一部分的下方。即使这在 Apple 的文档中并不是很清楚(摘录:“返回一个在 表格下方显示 的附件 View 。”)。

如果您希望页脚是静态的和非 float 的,您有两个简单的选择:

  • 不理想但简单:使用最后一节的页脚 View 作为静态页脚。这将在某些条件下工作:
  • 您的 UITableView 样式必须是 UITableViewStylePlain(因为 UITableViewStyleGrouped 对部分页眉/页脚提供与 UITableView tableHeaderViewtableFooterView
  • 相同的行为
  • 您将无法将最后一节页脚用于其真正目的:向特定节提供页脚信息

  • 这是一个简单的例子:
    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
        UIView *view = nil;
        if (section == [tableView numberOfSections] - 1) {
            // This UIView will only be created for the last section of your UITableView
            view = [[UIView alloc] initWithFrame:CGRectZero];
            [view setBackgroundColor:[UIColor redColor]];
        }
        return view;
    }
    
  • 目前最好的解决方案:在与 UITableView 相同的级别添加 UIView(在代码中或在您的 XIB 中)。一个小条件:
  • self.viewUIViewController 属性不能是 UITableView 对象。这意味着您不能继承 UITableViewController 而是 UIViewController 并使您的 Controller 符合 UITableViewDataSourceUITableViewDelegate 协议(protocol)。它实际上比看起来更简单,并且比直接使用 UITableViewController 更好(就我而言)。

  • 这是代码中的一个简单示例(但您可以使用 Interface Builder 执行完全相同的操作):

    View Controller .h:
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
    
    @end
    

    View Controller .m :
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @property (strong, nonatomic) UITableView *tableView;
    @property (strong, nonatomic) UIView *fixedTableFooterView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        CGFloat fixedFooterHeight = 200.0;
    
        // Initialize the UITableView
        CGRect tableViewFrame = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMinY(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - fixedFooterHeight);
        self.tableView = [[UITableView alloc] initWithFrame:tableViewFrame style:UITableViewStylePlain]; // or Grouped if you want...
        [self.tableView setDataSource:self];
        [self.tableView setDelegate:self];
        [self.view addSubview:self.tableView];
    
        // Initialize your Footer
        CGRect footerFrame = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMaxY(self.view.bounds) - fixedFooterHeight, CGRectGetWidth(self.view.bounds), fixedFooterHeight); // What ever frame you want
        self.fixedTableFooterView = [[UIView alloc] initWithFrame:footerFrame];
        [self.fixedTableFooterView setBackgroundColor:[UIColor redColor]];
        [self.view addSubview:self.fixedTableFooterView];
    }
    
    - (void)viewDidUnload {
        [super viewDidUnload];
        [self setTableView:nil];
        [self setFixedTableFooterView:nil];
    }
    
    @end
    

    您还可以指定 UIViewAutoresizing 掩码以使其在纵向和横向无缝工作,但我并没有使这个相当简单的代码复杂化。

    警告:这个 .h 和 .m 文件将无法编译,因为我没有放入 UITableViewDataSource 所需的方法。如果您想查看它的实际效果,请注释 setDataSource: 行。

    希望这会有所帮助,

    其他详情可以问我,

    关于ios - iOS中的静态页脚,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17612296/

    10-12 14:44
    查看更多