我有一个带有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文件,但没有相应的函数。我还有哪些其他选择?
编辑:我正在尝试在我的代码中实现以下内容: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()
}
heightForRow
在cellForRow
之前被调用,这就是为什么你得到了nil
。 - vadian