RxSwift + UITableViewCell如何获取heightForRowAt中的单元格对象 [英] RxSwift + UITableViewCell how to get cell object in heightForRowAt

查看:714
本文介绍了RxSwift + UITableViewCell如何获取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()
}

解决方案

调用tableView.cellForRow(at: indexPath)tableView(_: heightForRowAt:)中返回nil,因为在调用cellForRowAt之前,在特定的indexPath 上调用了heightForRowAt./p>

这里最好的选择是使用自定尺寸的电池. ( https://www.raywenderlich.com/129059/self-sizing -table-view-cells ).另一个选择是将像元的大小保留为模型的一部分,并在此处访问它们.

I have a view controller with UITableView. The table data is populated using 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)

and I have the following delegate function:

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
}

I want to access the cell object from inside 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?

EDIT: I am trying to achieve the following in my code:

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()
}

解决方案

The call tableView.cellForRow(at: indexPath) returns nil in tableView(_: heightForRowAt:) because heightForRowAt is called on a particular indexPath before the cellForRowAt is called.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆