简体   繁体   English

隐藏UITableView页脚

[英]Hiding UITableView footer

I am unable to hide my UITableView footer (ie setting it's height to 0 and animating the transition). 我无法隐藏我的UITableView页脚(即将其高度设置为0并为过渡设置动画)。

I tried to wrap tableView.tableViewFooter.height = 0 (and tableView.tableViewFooter = nil ) between [tableView beginUpdates] and [tableView endUpdates] but it doesn't work. 我试图在[tableView beginUpdates][tableView endUpdates]之间包装tableView.tableViewFooter.height = 0 (和tableView.tableViewFooter = nil ),但它不起作用。

Implementing the method below creates another footer. 实现下面的方法会创建另一个页脚。

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

在此输入图像描述

Is there a difference between a tableview footer and a section footer? tableview页脚和章节页脚之间有区别吗? I created mine by dropping a button under my tableview in my storyboard. 我在我的故事板中的桌面视图下面放了一个按钮,创建了我的。

Any ideas to do this simply? 有什么想法吗?

Ok here's how I solved this problem. 好的,这就是我如何解决这个问题。

- (void)refreshTableFooterButton
{
    if (should be visible) {
        self.tableView.tableFooterView = self.tableFooterButton;
    } else {
        self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    }
}

I used [[UIView alloc] initWithFrame:CGRectZero instead of nil to prevent unwanted empty cells to appear in the tableview. 我使用[[UIView alloc] initWithFrame:CGRectZero而不是nil来防止不需要的空单元格出现在tableview中。

I didn't need an animation block, but other people might. 我不需要动画块,但其他人可能会。

[UIView animateWithDuration:0.5 animations:^{

}];

Also, i had to keep a strong reference to my tableFooterButton IBOutlet. 此外,我必须对我的tableFooterButton IBOutlet保持强烈的引用。 That was part of why my initial attempt at solving the problem failed. 这是我解决问题的初步尝试失败的部分原因。 I'm not sure if having a strong reference to an IBOutlet is good practice though. 我不确定是否对IBOutlet有强烈的引用是好的做法。 Feel free to leave a comment about that. 随意发表评论。

使用以下代码:

self.tableView.tableFooterView?.hidden = false

This should do the trick. 这应该可以解决问题。 There are other alternatives such as this answer here . 还有其他的替代品,如这个答案在这里

tableView.sectionHeaderHeight = 0.0;
tableView.sectionFooterHeight = 0.0;

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1.0;


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

-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}

-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
    return [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}`

use following methods : 使用以下方法:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0f;
}

-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
    return [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}`

So in the end here's the simplest solution I can give you. 所以最后这是我能给你的最简单的解决方案。 So as you can see I just put the .m (for simplicity). 所以你可以看到我只是把.m(为了简单起见)。

There is a difference between table footer and section footer. 桌脚和部分页脚之间存在差异。 What you're looking for is the section footer. 您正在寻找的是页脚。

I also added an animation block for adding and removing it. 我还添加了一个动画块来添加和删除它。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

UIButton *_footerButton;


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createAndAddFooterButton];
}

-(void) buttonPressed:(id)button
{
    [UIView animateWithDuration:2 animations:^{
        self.tableView.tableFooterView = nil;
    }];
}

- (void)createAndAddFooterButton
{
    // here you create the button
    _footerButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_footerButton setTitle:@"Button Title" forState:UIControlStateNormal];
    [_footerButton sizeToFit];
    [_footerButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [UIView animateWithDuration:2 animations:^{
        self.tableView.tableFooterView = _footerButton;
    }];
}

@end

My problem was that the clear line was the footer which I could only define as 1 pixel high and not 0 (as I do not want to have a footer). 我的问题是,明确的行是页脚,我只能定义为1像素高而不是0(因为我不想有页脚)。 The only way I could fix it was to provide a view in viewForFooterInSection and set the background colour to the same as the header, so that visually you don't notice, and when I animate-in new rows they do not show through the top of the header where the clear line (footer) was. 我可以修复它的唯一方法是在viewForFooterInSection中提供一个视图,并将背景颜色设置为与标题相同,这样你就不会注意到,当我动画制作新行时,它们不会通过顶部显示清除行(页脚)所在的标题。

    override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let blankView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 1))
    blankView.backgroundColor = {Same colour as Header}
    return blankView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM