【问题标题】:How to add a footer to the UITableView?如何向 UITableView 添加页脚?
【发布时间】:2011-07-05 21:57:46
【问题描述】:

我正在使用此代码向 TableView 添加页脚。它有 20 个部分,每个部分有几行。有 titleForHeaderInSection 和 sectionForSectionIndexTitle 方法。

CGRect footerRect = CGRectMake(0, 0, 320, 40);
UILabel *tableFooter = [[UILabel alloc] initWithFrame:footerRect];
tableFooter.textColor = [UIColor blueColor];
tableFooter.backgroundColor = [self.theTable backgroundColor];
tableFooter.opaque = YES;
tableFooter.font = [UIFont boldSystemFontOfSize:15];
tableFooter.text = @"test";
self.theTable.tableFooterView = tableFooter;
[tableFooter release];

我做错了什么?

谢谢,

强化学习

【问题讨论】:

  • 你看到了什么?这段代码是viewForFooterInSection: 还是其他方法?
  • @BoltClock 页脚中没有任何内容。例如,该代码在 viewDidLoad 中。
  • @Rui:要制作节页脚,请实现viewForFooterInSection:。创建一个视图,并在委托方法中返回该视图。
  • @BoltClock 页脚用于表格,而不是表格的最后一部分。我实现该方法以便获得页脚?我在其他视图中也有相同的方式,不同之处在于我只有 1 个部分。谢谢,RL
  • @BoltClock 页脚用于表格。上面的代码适用于另一个表,但它只有一个部分。为什么?

标签: iphone uitableview uiview footer


【解决方案1】:

我在我的代码中特别看到了

self.theTable.tableFooterView = tableFooter;

工作和

[self.theTable.tableFooterView addSubview:tableFooter];

不起作用。所以坚持前者并在别处寻找可能的错误。 高温

【讨论】:

  • addSubview 不起作用,因为在这种情况下 tableFooterView 为零。
【解决方案2】:

你需要实现 UITableViewDelegate 方法

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

并为表格的相应部分返回所需的视图(例如,带有您希望在页脚中的文本的 UILabel)。

【讨论】:

  • 这个答案对于在每个部分中创建页脚是准确的,但我认为问题是关于为整个表格添加页脚,无论部分的数量如何。
  • @kris 仍然可以只计算总部分,如果 section == totalsection.count - 1 则显示页脚视图。
【解决方案3】:

我用过,效果很好:)

    UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)];
    [footerView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"ProductCellBackground.png"]]];
    self.tableView.tableFooterView = footerView;
    [self.tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    [self.tableView setContentInset:(UIEdgeInsetsMake(0, 0, -500, 0))];

【讨论】:

  • 如何在运行时更改页脚视图??
  • 你把这个具体放在哪里了? viewForFooterInSection 或其他地方?
  • @Adrian,绝对不在viewForFooterInSection 中,用于部分页脚,以上是tableView 不特定于任何部分。你可以把它放在viewDidLoad
【解决方案4】:

我知道这是一个很老的问题,但我刚刚遇到了同样的问题。我不知道确切原因,但似乎 tableFooterView 只能是 UIView 的一个实例(不是“种类”而是“是成员”)......所以在我的情况下,我创建了新的 UIView 对象(例如 wrapperView)并将我的自定义子视图添加到它...在您的情况下,更改您的代码:

CGRect footerRect = CGRectMake(0, 0, 320, 40);
UILabel *tableFooter = [[UILabel alloc] initWithFrame:footerRect];
tableFooter.textColor = [UIColor blueColor];
tableFooter.backgroundColor = [self.theTable backgroundColor];
tableFooter.opaque = YES;
tableFooter.font = [UIFont boldSystemFontOfSize:15];
tableFooter.text = @"test";
self.theTable.tableFooterView = tableFooter;
[tableFooter release];

到:

CGRect footerRect = CGRectMake(0, 0, 320, 40);
UIView *wrapperView = [[UIView alloc] initWithFrame:footerRect];

UILabel *tableFooter = [[UILabel alloc] initWithFrame:footerRect];
tableFooter.textColor = [UIColor blueColor];
tableFooter.backgroundColor = [self.theTable backgroundColor];
tableFooter.opaque = YES;
tableFooter.font = [UIFont boldSystemFontOfSize:15];
tableFooter.text = @"test";

[wrapperView addSubview:tableFooter];

self.theTable.tableFooterView = wrapperView;
[wrapperView release];
[tableFooter release];

希望对您有所帮助。它对我有用。

【讨论】:

  • 我有一个来自 XIB 的 UIView,并且在没有包装视图的情况下设置这个 UIView 不起作用。它仅在像您一样嵌套在另一个 UIView 中时才有效。看起来这是解决问题的嵌套/一些隐藏参数。
【解决方案5】:

一开始我只是在尝试这个方法:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

但是在使用它之后:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

问题已解决。示例程序-

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

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *sampleView = [[UIView alloc] init];
    sampleView.frame = CGRectMake(SCREEN_WIDTH/2, 5, 60, 4);
    sampleView.backgroundColor = [UIColor blackColor];
    return sampleView;
}

并包含 UITableViewDelegate 协议。

@interface TestViewController : UIViewController <UITableViewDelegate>

【讨论】:

  • 这是为每个部分而不是整个表格添加页脚。
【解决方案6】:

这些示例运行良好。您可以检查部分,然后返回一个高度以显示或隐藏部分。不要忘记从UITableViewDelegate 扩展您的视图控制器。

Objective-C

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0)
    {
        // to hide footer for section 0 
        return 0.0;
    }
    else
    {
        // show footer for every section except section 0 
        return HEIGHT_YOU_WANT;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = [UIColor blackColor];
    return footerView;
}

斯威夫特

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView()
    footerView.backgroundColor = UIColor.black
    return footerView
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    if section == 0 {
        // to hide footer for section 0
        return 0.0
    } else {
        // show footer for every section except section 0
        return HEIGHT_YOU_WANT
    }
}

【讨论】:

    【解决方案7】:

    Swift 2.1.1 下面的作品:

    func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
            let v = UIView()
            v.backgroundColor = UIColor.RGB(53, 60, 62)
            return v
        }
    
        func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
            return 80
        }
    

    如果使用self.theTable.tableFooterView = tableFooter,则最后一行和tableFooterView之间有一个空格。

    【讨论】:

      【解决方案8】:
      [self.tableView setTableFooterView:footerView];
      

      【讨论】:

        【解决方案9】:

        而不是

        self.theTable.tableFooterView = tableFooter;
        

        试试

        [self.theTable.tableFooterView addSubview:tableFooter];
        

        【讨论】:

        • 好的。没有警告。但问题无论如何都存在。 tableView 的页脚没有出现...
        • 视图没有出现,因为 -tableFooterView 默认为 nil。
        【解决方案10】:

        我遇到了同样的问题,但我在标题中替换了以下行:

        @interface MyController : UIViewTableViewController <UITableViewDelegate, UITableViewDataSource>
        

        使用这一行,它可以按预期工作:

        @interface RequestViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
        

        注意 UIViewController。 祝你好运:)

        【讨论】:

          【解决方案11】:

          如果你不喜欢粘底效果,我会把它放在viewDidLoad() https://stackoverflow.com/a/38176479/4127670

          【讨论】:

            猜你喜欢
            • 2021-07-06
            • 2012-03-25
            • 1970-01-01
            • 2011-01-31
            • 2018-07-09
            • 2022-01-23
            • 1970-01-01
            • 2012-12-03
            • 1970-01-01
            相关资源
            最近更新 更多