我有一个带有 UITableView 的视图控制器。表数据使用 RxSwift 填充:
let observable = Observable.just(data)
observable.bindTo(tableView.rx.items(cellIdentifier: "CategoryCell", cellType: CategoryCell.self)) { (row, element, cell) in
cell.setCategory(category: element)
}.disposed(by: disposeBag)
tableView.rx.setDelegate(self).disposed(by: disposeBag)
我有以下委托功能:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// cell = nil.
let cell = tableView.cellForRow(at: indexPath)
let screenWidth = UIScreen.main.bounds.size.width
let itemWidth = (screenWidth / 3.0) - 20
let itemHeight = (itemWidth) / 0.75 + 110
return itemHeight
}
我想从内部访问单元格对象heightForRowAt
但它给了我nil
。有办法进入这里的牢房吗?我一直在 RxCocoa 项目中查看 UITableView+Rx.swift 但没有这方面的功能。我还有什么其他选择?
EDIT:我试图在我的代码中实现以下目标:
class CategoryCell : UITableViewCell {
func calculateHeight() {
let screenWidth = UIScreen.main.bounds.size.width
let itemWidth = (screenWidth / 3.0) - 20
let itemHeight = (itemWidth) / 0.75 + 110
return itemHeight
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// cell = nil.
guard let cell : CategoryCell = tableView.cellForRow(at: indexPath) as? CategoryCell {
return 0.0
}
return cell.calculateHeight()
}
电话tableView.cellForRow(at: indexPath)
返回零tableView(_: heightForRowAt:)
因为 heightForRowAt 是在特定的 indexPath 上调用的before调用 cellForRowAt。
这里最好的选择是使用自调整大小的单元格。 (https://www.raywenderlich.com/129059/self-sizing-table-view-cells https://www.raywenderlich.com/129059/self-sizing-table-view-cells)。另一种选择是将单元格的大小保留为模型的一部分并在此处访问它们。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)