【发布时间】:2019-09-13 07:39:50
【问题描述】:
我正在尝试在其每个单元格中包含另一个 UITableView(次要)的 UITableView(主要)。主要和次要表视图具有自己的数据源对象类,并且 MajorDatasource 对象包含 MinorDatasource 对象的数组,如下所示。
我在每个类的UITableViewDatasource 函数中设置了断点,看起来MinorDatasource 的tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 函数从未被调用过,即使labels 数组包含对象。
我知道 UITableView 的 datasource 属性很弱,因此需要在某处对其进行强引用,但是,我认为在这种情况下这是正确的,我在这里做错了什么吗?
class MajorDatasource: NSObject, UITableViewDataSource {
var minorDatasources:[MinorDatasource]
init(minorDatasources:[MinorDatasource]) {
self.minorDatasources = minorDatasources
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return minorDatasources.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MajorTableViewCell")! as! MajorTableViewCell
cell.tableView.dataSource = minorDatasources[indexPath.row]
return cell
}
}
class MinorDatasource: NSObject, UITableViewDataSource {
var labels:[String]
init(labels:[String]) {
self. labels = labels
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return labels.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MinorTableViewCell")!
cell.textLabel?.text = labels[indexPath.row]
return cell
}
}
【问题讨论】:
-
所以你的意思是说'MinorDatasource'中的tableview dataSource方法没有被调用?
-
@nikBhosale numberOfRowsInSection 被调用(并返回,比如 2),但 cellForRowAt 没有被调用。
-
如果 numberOfRows 返回一些值,因为它们都是数据源方法,则不应发生这种情况。只有当 numberOfRows 返回 '0' 时才可能调用 numberOfRows 而不是 cellForRow
-
@nikBhosale 如果数据源由于某种原因被释放,则可能会发生这种情况,但是,我不确定为什么会出现这种情况,因为数据源是 minorDatasources 数组。
-
那么它会是“dequeueReusableCell”单元格吗?
标签: swift uitableview datasource