Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

ios - RxSwift reload tableview

let officialAccountObservable : Observable<[SearchUser]> = SearchAPI.sharedAPI.suggestAccounts()

        officialAccountObservable.bind(to: tableView.rx.items(cellIdentifier: "followcell", cellType: FollowCell.self)) {
            (index, user , cell) in
            if user.profileImagePath.isEmpty == false {
                cell.profile.af_setImage(withURL: URL.init(string: user.profileImagePath)!)
            }else {
                cell.profile.image = UIImage.init(named: "icon_user_03")
            }
            cell.nickName.text = user.nickName

            cell.follow.rx.tap
                .debounce(0.3, scheduler: MainScheduler.instance)
                .subscribe(onNext: {
                    [unowned self] in
                    cell.setFollow(user: user, completion: { (result) in
                        if(result == true){

                        }
                    })
                }).addDisposableTo(self.disposeBag)
        }.addDisposableTo(disposeBag)

func suggestAccounts() -> Observable<[SearchUser]> {

    let uri = Constants.VyrlSearchURL.suggestUsers

    return Observable.create { observer in
        let request = Alamofire.request(uri, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: Constants.VyrlAPIConstants.getHeader()).responseArray { (response: DataResponse<[SearchUser]>) in
            let value = response.result.value

            observer.onNext(value!)
            observer.onCompleted()
        }

        return Disposables.create(with: request.cancel)
    }
}

I want to reload the table view in code (result == true) For reload, OfficialAccountObservable must be received. My code is all over and I wonder how I can update it in that state.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

AFAIK, this is the standard way of doing this -

let source = PublishSubject<Observable<[SearchUser]>>()
let officialAccountObservable: Observable<[SearchUser]> = source.switchLatest()
source.onNext(suggestAccounts()) // every call will refresh your table

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...