这就是我想做的。
我想要里面的tableview
,返回动态计数。
例如,第一个单元格中有 3 行,第二个单元格有 2 行,第三个单元格有 4 行......就像这样。
而且 tableview
之外的计数也是动态的。
我应该返回什么tableView
单元格计数?
最佳答案
我认为你应该为 2 个 TableView 创建 2 个数据源。就像这样:
final class YouViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
let outerModel: [Any] = []
lazy var outerDatasource = OuterTableDatasource(model: self.outerModel)
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = outerDatasource
self.tableView.delegate = outerDatasource
}
}
final class CellThatContainTableView: UITableViewCell {
@IBOutlet weak var tableView: UITableView!
var dataSource: InnerTableDatasource! {
didSet {
self.tableView.dataSource = dataSource
self.tableView.delegate = dataSource
}
}
func configure(dataSource: InnerTableDatasource) {
self.dataSource = dataSource
}
}
final class OuterTableDatasource: NSObject {
var model: [Any]
init(model: [Any]) {
self.model = model
}
}
extension OuterTableDatasource: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return model.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//implement cell configuration
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CellThatContainTableView
cell.configure(dataSource: InnerTableDatasource(model: []))
return cell
}
}
extension OuterTableDatasource: UITableViewDelegate {
}
final class InnerTableDatasource: NSObject {
var model: [Any]
init(model: [Any]) {
self.model = model
}
}
extension InnerTableDatasource: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return model.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//implement cell configuration
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
return cell
}
}
extension InnerTableDatasource: UITableViewDelegate {
}
关于ios - 如何在swift中的动态tableview中创建动态tableview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52289949/