我无法为tableView添加分页。当我有更多项目时,在滚动底部时以及在向后滚动时我的分页都不会消失。我不知道如何在下面的代码中执行此操作。我正在等待您的帮助。我认为会帮助太多人。谢谢!
我的请求全部正确
// APIManager.swift
case .retailList(let cityId, let categoryId,let pageNumber):
value["filter"] = ["cityId":cityId,"categoryId":categoryId]
value["pageNumber"] = pageNumber;
value["size"] = 10;
break;
// PartnersView
viewModel.output.partnersResultObservable
.observeOn(mainScheduler)
.bind(to: tableView.rx.items(cellIdentifier: PartnersCell.defaultReuseIdentifier, cellType: PartnersCell.self)) {
row, element, cell in
cell.update(with: element);
}.disposed(by: viewModel.disposeBag);
viewModel.output.partnersResultObservable
.observeOn(mainScheduler)
.subscribe(onNext: { [weak self] _ in
self?.hideHud();
}).disposed(by: viewModel.disposeBag);
// PartnersViewModel.swift
struct Input {
let fetchingPartners: AnyObserver<Void>;
//2
let cityID: AnyObserver<String>;
let catID: AnyObserver<String>;
let pageNumber: AnyObserver<String>;
}
struct Output {
let partnersResultObservable: Observable<[PartnersItem]>;
let errorsObservable: Observable<ErrorResponse>;
}
let input: Input;
let output: Output;
//Input
private let fetchingPartnersSubject = PublishSubject<Void>();
private let cityIDSubject = PublishSubject<String>();
private let catIDSubject = PublishSubject<String>();
private let pageNumberSubject = PublishSubject<String>();
//Output
private let partnersResultSubject = PublishSubject<[PartnersItem]>();
private let errorSubject = PublishSubject<ErrorResponse>();
let disposeBag = DisposeBag();
init(service: PartnersService) {
input = Input(fetchingPartners: fetchingPartnersSubject.asObserver(), cityID: cityIDSubject.asObserver(), catID: catIDSubject.asObserver(), pageNumber: pageNumberSubject.asObserver());
output = Output(partnersResultObservable: partnersResultSubject.asObserver(), errorsObservable: errorSubject.asObserver());
fetchingPartnersSubject.withLatestFrom(partnersFilterCredentionals).observeOn(ConcurrentMainScheduler.instance).flatMapLatest { credentials in
return service.fetchAllPartners(partnersFilterCredentionals: credentials).materialize()
}.subscribe(onNext: { [weak self] event in
switch event {
case .next(let data):
self?.partnersResultSubject.onNext(data);
break;
case .error(let error):
self?.errorSubject.onNext(error as! ErrorResponse);
break;
default:
break;
}
}).disposed(by: disposeBag);
}
private var partnersFilterCredentionals: Observable<PartnersFilterCredentionals> {
return Observable.combineLatest(cityIDSubject.asObservable(), catIDSubject.asObservable(),pageNumberSubject.asObserver()) { cityID, catID , pageNumber in
return PartnersFilterCredentionals(cityID: cityID, catID: catID,pageNumber: pageNumber);
}
}
}
// func fetchAllPartners
func fetchAllPartners(partnersFilterCredentionals: PartnersFilterCredentionals) -> Observable<[PartnersItem]> {
return Observable.create { observer in
//fetch data from server
if Disk.exists(FileName.partners, in: .caches) {
do {
let partners = try Disk.retrieve(FileName.partners, from: .caches, as: [PartnersItem].self);
observer.onNext(partners);
} catch {
debugPrint("error \(error.localizedDescription)");
}
}
let request = javaNetworkProvider.request(.retailList(Int(partnersFilterCredentionals.cityID)!,Int(partnersFilterCredentionals.catID)!,Int(partnersFilterCredentionals.pageNumber)!), callbackQueue: pillikanQueue, progress: nil, completion: { result in
if let value = result.value {
do {
let response = try JSONDecoder().decode(PartnersResponse.self, from: value.data);
let list = response.retailList;
observer.onNext(list);
try Disk.save(list, to: .caches, as: FileName.partners);
debugPrint("object: \(response)");
} catch let error {
debugPrint("error \(error.localizedDescription)");
if let e = try? JSONDecoder().decode(ErrorResponse.self, from: value.data) {
observer.onError(e);
}
}
}
})
return Disposables.create {
request.cancel();
}
}.subscribeOn(ConcurrentDispatchQueueScheduler(qos: .userInteractive));
}