我们能否实现具有动态原型(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/