【问题标题】:Hiding UITableView footer隐藏 UITableView 页脚
【发布时间】:2014-04-08 01:43:28
【问题描述】:

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

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

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

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

tableview 页脚和节页脚之间有区别吗?我通过在我的故事板中的 tableview 下放置一个按钮来创建我的。

有什么想法可以简单地做到这一点吗?

【问题讨论】:

  • 使用 tableview 委托方法并在此处设置高度 0。或者放一段代码。
  • @Nico tableview 页脚和节页脚之间有区别吗?因为使用这种方法会创建另一个页脚。

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


【解决方案1】:

好的,这就是我解决这个问题的方法。

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

我使用[[UIView alloc] initWithFrame:CGRectZero 而不是nil 来防止不需要的空单元格出现在表格视图中。

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

[UIView animateWithDuration:0.5 animations:^{

}];

另外,我必须强烈引用我的 tableFooterButton IBOutlet。这就是我最初尝试解决问题失败的部分原因。我不确定强烈引用 IBOutlet 是否是一种好习惯。欢迎对此发表评论。

【讨论】:

  • 虽然对IBOutlet 的强引用不是标准模式,但在这种情况下保持强引用是可以的,因为您可能会再次将相同的视图放回视图层次结构中之后。强引用避免了以某种方式重新创建视图。
【解决方案2】:

使用以下代码:

self.tableView.tableFooterView?.hidden = false

【讨论】:

  • 叮叮叮我们赢了
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    使用以下方法:

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

    【讨论】:

      【解决方案5】:

      所以最后这是我能给你的最简单的解决方案。如您所见,我只是放了 .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
      

      【讨论】:

      • tableview 页脚和节页脚有区别吗?因为使用这种方法会创建另一个页脚。
      • 不,没有区别。这个页脚应该很不起眼,你能提供截图吗?
      • 也尝试将其设置为 0,看看效果如何。
      • 我没有足够的声望在这里发布图片,但这里是i59.tinypic.com/ivvbt3.png,返回值为 50。
      • 现在我有一个问题要问你,你想要一个页脚吗?
      【解决方案6】:

      我的问题是清晰的线是页脚,我只能定义为 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
      

      【讨论】:

        猜你喜欢
        • 2017-01-24
        • 2012-07-11
        • 2019-02-19
        • 2011-02-23
        • 2013-01-11
        • 2020-01-23
        • 1970-01-01
        • 2019-09-02
        • 2010-12-04
        相关资源
        最近更新 更多