【问题标题】:How to remove UITableView section header separator如何删除 UITableView 部分标题分隔符
【发布时间】:2017-07-06 21:09:19
【问题描述】:

我想删除(或将它们设为 clearColor)UITableView 的部分标题分隔符。设置tableView.separatorStyle = .none 不起作用。我也尝试过here 的解决方案,但它们都没有真正为我工作(可能是因为答案很老了)。我仍然在部分标题下方看到这个小分隔符。

我在 Storyboard 中创建了 UITableView 并在那里添加了一个 UITableViewCell。然后我将它设置为这样的标题:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let cell = tableView.dequeueReusableCell(withIdentifier: "headerTableViewCell")
        return cell
    }

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 1 {
            return 49
        }
        return 0
    }

【问题讨论】:

  • 为什么要在 viewForHeaderInSection 中添加单元格?

标签: ios swift uitableview storyboard


【解决方案1】:

不知道您为什么在viewForHeaderInSection 方法中返回UITableViewCell,因此可能显示了该UITableViewCell 的分隔符。尝试从viewForHeaderInSection 方法返回单元格的contentView 而不是单元格。

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let cell = tableView.dequeueReusableCell(withIdentifier: "headerTableViewCell")
    return cell.contentView
}

【讨论】:

    【解决方案2】:

    试试这个:

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView()
        headerView.backgroundColor = UIColor.clear
        return headerView
    }
    

    【讨论】:

      【解决方案3】:

      我已经通过以下方式解决了这个问题:

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

      【讨论】:

      • 其实这是一个很好的方法。虽然你可以做return 0
      • 在某些情况下,0 会被忽略(分组视图)。您应该返回 CGFloat.leastNormalMagnitude
      【解决方案4】:

      结果覆盖layoutSubviews 删除所有单元格的分隔符,包括部分相关的和单元格相关的。

      @interface MyCustomCell : UITableViewCell
      
      @end    
      
      @implementation MyCustomCell
      
      - (void)layoutSubviews {
          // don't call [super layoutSubviews]
          // can be left empty
          // does not harm other UI elements inside the contentView
      }
      
      @end
      

      在多单元格部分中,您可能只想删除与部分相关的分隔符(在第一个单元格的顶部和最后一个单元格的底部),保持插入的单元格间分隔符显示:

      - (void)layoutSubviews {
          [super layoutSubviews];
      
          for (UIView *view in self.subviews) {
              if ([view isEqual:self.contentView]) continue;
      
              view.hidden = view.bounds.size.width == self.bounds.size.width;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-14
        • 2012-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-12
        • 2023-03-07
        相关资源
        最近更新 更多