首页 > 解决方案 > 提高具有非统一内容的 `UITableViewCell` 的性能

问题描述

我有一个UITableView其单元格包含动态内容。

假设可能有三种类型的单元格内容(实际上,还有更多)。

Type1: 3 个UILabel堆叠在一起

Type2: 2 个UIButtons并排堆叠

Type3: 1UISlider我是中心

我有这些 UI 元素的类。

在我的cellForRowAtIndexPath中,我将这些添加到contentView

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCell(withIdentifier: cellID)!
  cell.contentView.subviews.forEach { $0.removeFromSuperview() } // Remove everything before adding new stuff       
  cell.contentView.addSubview(Type3())  // Pretend there's logic here to decide whether to use Type1, Type2, or Type3
  return cell
}

这目前有效,但显然非常hacky且不优雅。我该怎么办?

标签: iosswiftuitableviewautolayoutuistackview

解决方案


推荐阅读