ios - 动态可扩展表格 View

标签 ios swift uitableview

我们能否实现具有动态原型(prototype)单元格的 Expandable UITableView,它可以扩展其中的动态单元格数量?喜欢:

我在 tableView 中有“x”个类(class)。点击任何类(class)后,它将扩展“y”个接受该类(class)的科目。

我的 tableView 中没有任何静态单元格。我怎样才能做到这一点?

最佳答案

您可以使用 ExpyTableView ,它从您给定的标题单元格中创建一个可扩展部分。向下兼容 iOS 8.0。

您所要做的就是导入 ExpyTableView,然后:

class ViewController: ExpyTableViewDataSource, ExpyTableViewDelegate {

  @IBOutlet weak var expandableTableView: ExpyTableView!

  // First, set data source and delegate for your table view.
  override func viewDidLoad() {
    super.viewDidLoad() 
    expandableTableView.dataSource = self
    //expandableTableView.delegate = self
  }

  // Then return your expandable cell instance from expandingCell data source method.
  func expandableCell(forSection section: Int, inTableView tableView: ExpyTableView) -> UITableViewCell {
    // this cell will be displayed at IndexPath with section: section and row 0
  }
} 

您可以看到您以前的表格 View 部分现在是一个可扩展的表格 View 部分。您还可以下载示例项目并查看更详细的示例。

在示例项目中,有一个适合您需要的具体示例。您只需添加以下代码:

    func numberOfSections(in tableView: UITableView) -> Int {
        return x //x is your provided number here.(e.g. number of courses)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return y //y is your provided number here.(number of subjects related to a course)
    }

关于ios - 动态可扩展表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39915231/

相关文章:

ios - 按钮不工作取决于变量的长度

arrays - 为什么 Swift Array<Int> 与 AnyObject 兼容?

ios - 同时更改所有表格 View 单元格上的显示参数

ios - 带有 Segues 到多个 View 的 TableView

ios - 我想获取屏幕左侧的后退按钮,但无法获取它

ios - Google maps iOS SDK 显示了选择多边形的默认选项

ios - ReactiveSwift 中运算符 '<~' 的使用不明确

ios - UIButton 闪烁正常工作,但按钮操作不工作

arrays - 获取通过数组中的 http 调用接收到的 json - Swift iOS

iOS:如何将滑动删除按钮添加到 viewForHeaderInSection 中的自定义 View