【问题标题】:Hide footer view in UITableView在 UITableView 中隐藏页脚视图
【发布时间】:2012-07-11 19:37:23
【问题描述】:

我一直在努力隐藏页脚视图。我的问题是,当我单击按钮时,页脚中有一个按钮章节更新后。

footerView.hidden = YES

我在按钮操作中使用了它,但它不起作用。

【问题讨论】:

    标签: iphone uitableview ios5 footer


    【解决方案1】:

    应该这样做

    tableView.tableFooterView.hidden = YES;
    

    【讨论】:

    • 我不认为你试图隐藏“footerView”,而是隐藏该部分的最后一个单元格。我的回答正确显示了如何隐藏 footerView
    • 它没有隐藏 footerview man 。下一部分在 footerview 之后。
    • 页脚视图有两种:tableFooterView 和 sectionFooterView。因此混乱......
    • 要让它工作,你应该这样做,- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { [tableView.tableFooterView setHidden:YES]; return 1.0f; } 关键是返回 1.0f
    • 这不起作用。部分页脚没有隐藏,甚至没有减少到 0 高度。
    【解决方案2】:

    有四种解决方案。他们是,

    解决方案 1:

    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];
    }
    
    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger )section {
        return [[UIView alloc] initWithFrame:CGRectZero];
    }
    

    解决方案 2:

    您可以通过界面生成器在尺寸选项卡下设置页脚/页眉高度。

    解决方案 3:

    设置 contentInset 属性。

    self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, -20, 0);
    

    用于使顶部和底部接触边缘。

    解决方案 4:

    实现以下,根据您的条件设置值。 0.0 将不被接受。较低的值应为 1.0。

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger )section {
        if(section == 0) {
           return 6;
        } else {
           return 1.0;
        }
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger )section {
        return 5.0;
    }
    
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger )section {
        return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    }
    
    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger )section {
        return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    }
    

    【讨论】:

    【解决方案3】:

    这个可以 通过实现委托方法

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

    【讨论】:

      【解决方案4】:

      不使用自定义视图时,从tableView:titleForFooterInSection: 返回nil,从tableView:heightForFooterInSection: 返回0

      @property(nonatomic, strong) NSString *footerTitle;
      @property(nonatomic, assign) BOOL shouldHideFooter;
      
      - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
      {
          return self.shouldHideFooter ? nil : self.footerTitle;
      }
      
      - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
      {
          return self.shouldHideFooter ? 0 : UITableViewAutomaticDimension;
      }
      

      【讨论】:

        【解决方案5】:

        Swift 3.0 解决方案,该解决方案还删除了前一个单元格和后一个单元格之间令人讨厌的 1px 边框。

        override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
            return CGFloat.leastNormalMagnitude
        }
        
        override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
            return nil
        }
        

        【讨论】:

        • 我必须根据 HTTP 请求显示/隐藏页脚。我如何应用更改?因为您的示例最初是执行的,我猜。
        【解决方案6】:

        参考@J.Hong的回答,Swift4版本(iOS 11.3):

        func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
            return .leastNonzeroMagnitude
        }
        

        与@Tim Newton 的回答相比:

        • 无需拨打titleForFooterInSection
        • 简化 CGFloat.leastNormalMagnitude -> .leastNonzeroMagnitude(更 Swifty IMO)

        【讨论】:

          【解决方案7】:

          在 swift4 中:

          self.tableView.tableFooterView = nil
          

          【讨论】:

            【解决方案8】:

            Swift 5 解决方案:(隐藏部分页脚,您可能需要另一块来摆脱表格页脚)

            func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
                return 0.0
            }
            
            func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
                return UIView()
            }
            

            【讨论】:

            • 这个帮助了我。谢谢。
            猜你喜欢
            • 2014-04-08
            • 1970-01-01
            • 2017-01-24
            • 1970-01-01
            • 2019-02-19
            • 2016-11-05
            • 2015-03-28
            • 2017-02-06
            • 1970-01-01
            相关资源
            最近更新 更多