【问题标题】:If I hide a tableView cell it still takes up space. How do I remove it entirely?如果我隐藏一个 tableView 单元格,它仍然会占用空间。如何完全删除它?
【发布时间】:2021-03-28 02:56:07
【问题描述】:

我有一个tableView 和两个cells,代码如下:

viewModel.content.bind(to: tableView.rx.items) { _, _, item in
    return self.cellFactory(item: item)
}.disposed(by: disposeBag)

func cellFactory(item: Any) -> UITableViewCell {
    if let cellViewModel = item as? StaticSupportTableViewCellViewModel {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: StaticSupportTableViewCell.identifier) as? StaticSupportTableViewCell else { return UITableViewCell() }
        cell.viewModel = cellViewModel
        cell.isHidden = true
        return cell
    } else if let cellViewModels = item as? SupportTableViewCellViewModel {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: SupportTableViewCell.identifier) as? SupportTableViewCell else { return UITableViewCell() }
        cell.viewModel = cellViewModels
        return cell
    } else { return UITableViewCell() }
}

现在,我想隐藏第一个单元格并写cell.isHidden = true。虽然这确实隐藏了单元格,但它仍然占用了不可见的空间,将另一个 cell 向下推。如何让它完全消失?我试过cell.removeFromSuperView() 但没有运气。我还尝试使用estimatedHeightForRowAt 委托和单元格本身设置高度。

【问题讨论】:

  • 我在任何地方都没有看到 cell.isHidden = true。
  • 我后来添加它只是为了测试,它没有工作:) 这是标准代码。
  • 声明要在函数外部删除的单元格变量(不让),然后再次尝试将其从超级视图中删除。

标签: ios swift uitableview tableview show-hide


【解决方案1】:

我有 2 条建议:

1.如果你的单元格的尺寸是动态的,你可以使用约束 重置 StaticSupportTableViewCell 大小。我推荐 StackView 为此。


2.如果你的cell有静态高度,你可以在heightForRowAt函数中设置0。

代码示例适用于 RxSwift

像这样为所有 CellViewModel 定义高度值:

// MARK: - HeightDefinable

extension StaticSupportTableViewCellViewModel: HeightDefinable {

   static func defaultHeight() -> CGFloat {
       return 60
   }
}

设置 TableView 委托

tableView.rx.setDelegate(self).disposed(by: disposeBag)

设置高度

在这里,通过使用从 viewModel.content 项目中获得的信息,您可以通过根据您想要的条件将单元格高度设置为 0 来使其隐藏。这是一个例子。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    guard let element = viewModel.content.value?[indexPath.row] else {
        return UITableView.automaticDimension
    }
    
    if indexPath.row == 0 {
        
        return 0
    }

    return type(of: element).defaultHeight()
}

为了能够将其用作 viewModel.content.value,内容必须定义为 BehaviorRelay。

【讨论】:

    【解决方案2】:

    我认为这里的问题就像当你试图将一棵树隐藏在森林中时,你不能直接做到这一点,如果你想完全隐藏它,你需要从它的根部做到完全消失,我认为这是同样的问题,您需要做的就是从数组中删除不需要的项目或将其移动到另一个数组,然后返回其计数并重新加载 tableView

    【讨论】:

      【解决方案3】:

      我不确定您是否尝试实现,但如果您根本不想要它,请尝试过滤您的 viewModel.content 或者如果您有条件隐藏特定项目,请在绑定之前将其应用到 filter 中.

      例如:

      viewModel.content
      .filter{!($0 is StaticSupportTableViewCellViewModel)}
      .bind(to: tableView.rx.items) { _, _, item in
          return self.cellFactory(item: item)
      }.disposed(by: disposeBag)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-25
        • 1970-01-01
        • 1970-01-01
        • 2012-06-10
        • 2011-03-04
        • 1970-01-01
        • 2018-09-12
        • 2015-07-16
        相关资源
        最近更新 更多