RxSwift + UITableViewCell如何获取heightForRowAt中的单元格对象 [英] RxSwift + UITableViewCell how to get cell object in heightForRowAt
问题描述
我有一个带有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()
}
调用 这里最好的选择是使用自定尺寸的电池. ( https://www.raywenderlich.com/129059/self-sizing -table-view-cells ).另一个选择是将像元的大小保留为模型的一部分,并在此处访问它们. I have a view controller with UITableView. The table data is populated using RxSwift: and I have the following delegate function: I want to access the cell object from inside EDIT: I am trying to achieve the following in my code:
The call The best bet here would be to use self-sizing cells. (https://www.raywenderlich.com/129059/self-sizing-table-view-cells). Another option would be to keep the size of the cells as part of your model and access them here. 这篇关于RxSwift + UITableViewCell如何获取heightForRowAt中的单元格对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!tableView.cellForRow(at: indexPath)
在tableView(_: heightForRowAt:)
中返回nil,因为在调用cellForRowAt之前,在特定的indexPath 上调用了heightForRowAt./p>
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
but it gives me nil
. Is there a way to access the cell here? I have been looking at UITableView+Rx.swift in RxCocoa project but there is no function for this. What other options do I have?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)
returns nil in tableView(_: heightForRowAt:)
because heightForRowAt is called on a particular indexPath before the cellForRowAt is called.