如何在UITableView中添加页脚?

Rui*_*pes 56 iphone footer uitableview uiview

我使用此代码向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];
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

谢谢,

RL

kri*_*ris 36

我在我的代码中特别注意到了

self.theTable.tableFooterView = tableFooter;
Run Code Online (Sandbox Code Playgroud)

工作和

[self.theTable.tableFooterView addSubview:tableFooter];
Run Code Online (Sandbox Code Playgroud)

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

  • addSubview不起作用,因为在这种情况下tableFooterView是nil. (42认同)

Eri*_*rik 36

您需要实现UITableViewDelegate方法

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)

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

  • 这个答案对于在每个部分中创建页脚是准确的,但我认为问题是关于为整个表添加页脚,而不管部分的数量. (21认同)

paw*_*ni1 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))];
Run Code Online (Sandbox Code Playgroud)


zio*_*lek 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];
Run Code Online (Sandbox Code Playgroud)

至:

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];
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.这个对我有用.


小智 6

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

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)

但在使用之后:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)

问题解决了.样本计划 -

- (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;
}
Run Code Online (Sandbox Code Playgroud)

并包含UITableViewDelegate协议.

@interface TestViewController : UIViewController <UITableViewDelegate>
Run Code Online (Sandbox Code Playgroud)


abd*_*lek 5

这些样本效果很好。您可以检查部分,然后返回高度以显示或隐藏部分。不要忘记从 扩展你的视图控制器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;
}
Run Code Online (Sandbox Code Playgroud)

迅速

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
    }
}
Run Code Online (Sandbox Code Playgroud)


ama*_*ttn 1

代替

self.theTable.tableFooterView = tableFooter;
Run Code Online (Sandbox Code Playgroud)

尝试

[self.theTable.tableFooterView addSubview:tableFooter];
Run Code Online (Sandbox Code Playgroud)

  • 该视图不会出现,因为 -tableFooterView 默认为零。 (5认同)