简体   繁体   English

带有 RxCocoa 的 UITableView 没有正确观察 contentOffset 属性

[英]UITableView with RxCocoa is not observing correctly to contentOffset property

I have an issue with observing the contentOffset property of UITableView with RxCocoa.我在使用 RxCocoa 观察 UITableView 的 contentOffset 属性时遇到问题。

I've tried RxCocoa property:我试过 RxCocoa 属性:

view.tableView.rx.contentOffset
                .mapAt(\.y)
                .subscribe(onNext: { print($0) })

And in console I see (0, 0) once and nothing else.在控制台中,我只看到(0, 0)一次,仅此而已。

I've tried to replace it with code from RxCocoa:我试图用 RxCocoa 的代码替换它:

ControlProperty(
                values: BehaviorSubject<CGPoint>(value: RxScrollViewDelegateProxy.proxy(for: view.tableView).scrollView?.contentOffset ?? CGPoint.zero),
                valueSink: Binder(view.tableView) { scrollView, contentOffset in
                        scrollView.contentOffset = contentOffset
                    }
                )
                .subscribe(onNext: { print("myOffset", $0) })

And got the same result: myOffset (0, 0) once and nothing else.并得到了相同的结果: myOffset (0, 0)一次,仅此而已。

I've tried to observe other property and haven't got anything:我试图观察其他财产并没有得到任何东西:

view.tableView.rx.didScroll
                .subscribe(onNext: { print(view.tableView.contentOffset) })

BUT.但。 I've tried to add Observable interval:我尝试添加 Observable 间隔:

Observable<Int>.interval(1, scheduler: MainScheduler.instance)
                .subscribe(onNext: { _ in print(view.tableView.contentOffset) })

And each second I've got different points: (0, 0), (0, 38), (0, 64) .每一秒我都有不同的点: (0, 0), (0, 38), (0, 64)

I'm using: RxCocoa (5.0.0);我正在使用:RxCocoa (5.0.0); RxSwift (5.0.0) RxSwift (5.0.0)

You haven't shown us the code that is actually causing the problem.您还没有向我们展示实际导致问题的代码。 Note that the below works perfectly:请注意,以下工作完美:

final class ViewController: UIViewController {
    private var tableView: UITableView!
    private let disposeBag = DisposeBag()

    override func loadView() {
        super.loadView()
        tableView = UITableView(frame: view.bounds)
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        view.addSubview(tableView)
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        tableView.frame = view.bounds
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.rx.contentOffset
            .map { $0.y }
            .bind(onNext: { print($0) })
            .disposed(by: disposeBag)

        Observable.just(Array.init(repeating: "Item", count: 35))
            .bind(to: tableView.rx.items) { (tableView, row, element) in
                let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
                cell.textLabel?.text = "\(element) @ row \(row)"
                return cell
            }
            .disposed(by: disposeBag)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM