隐藏UITableView页脚

时间:2014-03-05 19:25:17

标签: ios objective-c uitableview cocoa-touch uikit

我无法隐藏我的UITableView页脚(即将其高度设置为0并为过渡设置动画)。

我尝试将tableView.tableViewFooter.height = 0(和tableView.tableViewFooter = nil)包裹在[tableView beginUpdates][tableView endUpdates]之间,但它不起作用。

实现下面的方法会创建另一个页脚。

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

enter image description here

tableview页脚和章节页脚之间有区别吗?我在我的故事板中的桌面视图下面放了一个按钮,创建了我的。

任何想法都可以做到这一点吗?

6 个答案:

答案 0 :(得分:9)

好的,我是如何解决这个问题的。

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

我使用[[UIView alloc] initWithFrame:CGRectZero代替nil来防止不需要的空单元格出现在tableview中。

我不需要动画块,但其他人可能。

[UIView animateWithDuration:0.5 animations:^{

}];

另外,我必须对我的tableFooterButton IBOutlet保持强烈的引用。这是我解决问题的初步尝试失败的部分原因。我不确定是否对IBOutlet有强烈的引用是好的做法。随意发表评论。

答案 1 :(得分:8)

使用以下代码:

self.tableView.tableFooterView?.hidden = false

答案 2 :(得分:2)

这应该可以解决问题。还有其他替代方案,例如此答案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];
}`

答案 3 :(得分:1)

使用以下方法:

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

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

答案 4 :(得分:0)

所以最后这里是我能给你的最简单的解决方案。所以你可以看到我只是把.m(为了简单起见)。

表格页脚和章节页脚之间存在差异。你要找的是章节页脚。

我还添加了一个动画块来添加和删除它。

#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

答案 5 :(得分:0)

我的问题是,清晰的行是页脚,我只能定义为1像素高而不是0(因为我不想有页脚)。 我可以修复它的唯一方法是在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
相关问题