5

i want know end of reloadTableView Then I want to scroll down to the bottom of the table view.

Before I used RxSwift Just after reloadData It was possible using setContentOffSet or ScrollToRow.

I tried it with the code I found. never called endUpdates.

var replyList : BehaviorRelay<[Reply]>!

func bind(){

    replyViewModel.replyList
       .asDriver()
       .drive(replyTableView.rx.items){ [weak self] (tableView,row,item) in
           return self?.makeReplyCell(tableView: tableView, replyInfo: item) ?? UITableViewCell()

                }
       .disposed(by: disposeBag)

    replyTableView.endUpdatesEvent
        .asObservable()
        .subscribe({ _ in
            print("Scroll To Bottom")
        })
        .disposed(by: disposeBag)
}

import Foundation
import RxCocoa
import RxSwift

extension UITableView {

    /// Reactive wrapper for `UITableView.insertRows(at:with:)`
    var insertRowsEvent: ControlEvent<[IndexPath]> {
        let source = rx.methodInvoked(#selector(UITableView.insertRows(at:with:)))
                .map { a in
                    return a[0] as! [IndexPath]
                }
        return ControlEvent(events: source)
    }

    /// Reactive wrapper for `UITableView.endUpdates()`
    var endUpdatesEvent: ControlEvent<Bool> {
        let source = rx.methodInvoked(#selector(UITableView.endUpdates))
                .map { _ in
                    return true
                }
        return ControlEvent(events: source)
    }

    /// Reactive wrapper for when the `UITableView` inserted rows and ended its updates.
    var insertedItems: ControlEvent<[IndexPath]> {
        let insertEnded = Observable.combineLatest(
                insertRowsEvent.asObservable(),
                endUpdatesEvent.asObservable(),
                resultSelector: { (insertedRows: $0, endUpdates: $1) }
        )
        let source = insertEnded.map { $0.insertedRows }
        return ControlEvent(events: source)
    }
}
oijafoijf asnjksdjn
  • 1,115
  • 12
  • 35
  • I do not know is it possible in rxswift. But maybe you can look on observers provided by uitableview instead of dataArray. – khunshan Jul 24 '19 at 09:05

1 Answers1

0

UITableView.endUpdates() won't help you here as you are not calling it directly and under the hood it might be not called at all (at least with the rx wrapper).

More legitimate solution would be to observe for invocations of layoutSubviews() method where you can validate cells if they were reloaded at this layout cycle and they are present - then you can do scroll to them for example. If layoutSubviews() were called but the cells are not there yet in the table view then you wait for the next cycle and check it again.