如何在UITableView中添加页脚?

时间:2011-02-28 16:42:48

标签: iphone uitableview uiview footer

我正在使用此代码向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];

我做错了什么?

感谢,

RL

11 个答案:

答案 0 :(得分:36)

我在代码中特别注意到

self.theTable.tableFooterView = tableFooter;

有效,

[self.theTable.tableFooterView addSubview:tableFooter];

不起作用。所以坚持前者,并寻找其他可能的错误。 HTH

答案 1 :(得分:36)

您需要实现UITableViewDelegate方法

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

并为表格的相应部分返回所需的视图(例如,在页脚中包含您想要的文本的UILabel)。

答案 2 :(得分:18)

我使用了它并且完美地工作了:)

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

答案 3 :(得分:6)

我知道这是一个非常古老的问题,但我刚遇到同样的问题。我不知道为什么,但似乎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];

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

答案 4 :(得分:6)

最初我只是尝试了这个方法:

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

答案 5 :(得分:4)

有效的

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之间会有一个空格。

答案 6 :(得分:3)

这些样本效果很好。您可以检查部分,然后返回高度以显示或隐藏部分。不要忘记从UITableViewDelegate扩展您的viewcontroller。

<强>目标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 :(得分:2)

[self.tableView setTableFooterView:footerView];

答案 8 :(得分:1)

而不是

self.theTable.tableFooterView = tableFooter;

尝试

[self.theTable.tableFooterView addSubview:tableFooter];

答案 9 :(得分:1)

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

@interface MyController : UIViewTableViewController <UITableViewDelegate, UITableViewDataSource>

使用此行并按预期工作:

@interface RequestViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

注意UIViewController。 祝你好运:)

答案 10 :(得分:0)

如果您不喜欢粘性底部效果,我会将其放入viewDidLoad() https://stackoverflow.com/a/38176479/4127670

相关问题