RxDataSources 上拉刷新下拉加载

//

//  TestViewController.swift

//  TaskElephant

//

//  Created by apple on 17/4/13.

//  Copyright © 2017年xiangguohe. All rights reserved.

//

importUIKit

#if!RX_NO_MODULE

importRxSwift

importRxCocoa

importRxDataSources

#endif

importMJRefresh

classTestViewController:UIViewController{

vardisposeBag =DisposeBag()

@IBOutletvartableView:UITableView!

lazyvarcoachViewModel:CoachViewModel= {

letcoachViewModel =CoachViewModel()

returncoachViewModel

}()

vardataSource =RxTableViewSectionedReloadDataSource>()

vardataArr =Variable([SectionModel]())

varpage =1

varheader =MJRefreshNormalHeader()

varfooter =MJRefreshAutoNormalFooter()

overridefuncviewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view.

ifIOS7{

self.edgesForExtendedLayout= .top

}

view.backgroundColor=whiteColor

self.automaticallyAdjustsScrollViewInsets=false

rx_requestAllViewControllerData()

rx_setupRxDataSourcesForTestViewController()

rx_addMoreAndRefresh()

}

overridefuncdidReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

extensionTestViewController{

funcrx_requestAllViewControllerData() ->Void{

ifpage==1{

self.dataArr.value.removeAll()

}

letparameterDic = [

"page":page,

"pageSize":"10",

"sportId":"",

]as[String:Any]

/*

coachViewModel.request(RequestType.requestTypePOST, urlstring: getCoachListBySportIdUrl, paramete: parameterDic as [String : AnyObject])

.bindTo(tableView.rx.items(dataSource: dataSource))

.addDisposableTo(disposeBag)

*/

coachViewModel.request(RequestType.requestTypePOST, urlstring:getCoachListBySportIdUrl, paramete: parameterDicas[String:AnyObject])

.subscribe(onNext: { (section : [SectionModel])in

self.dataArr.value+= section

})

.addDisposableTo(disposeBag)

}

}

extensionTestViewController:UITableViewDelegate{

funcrx_setupRxDataSourcesForTestViewController() ->Void{

self.tableView.hideTableFooterView()

dataSource.configureCell= {

_, tableView, indexPath, coachModelin

letcell = tableView.dequeueReusableCell(withIdentifier:"testTableViewCell", for: indexPath)as!TestTableViewCell

cell.tag= indexPath.row

cell.cellForCoachModel(model: coachModel)

returncell

}

//绑定数据

dataArr

.asObservable()

.bindTo(tableView.rx.items(dataSource:dataSource))

.addDisposableTo(disposeBag)

tableView.rx

.setDelegate(self)

.addDisposableTo(disposeBag)

tableView.rx.itemSelected

.map{ [weakself] indexPathin

return(indexPath,self?.dataSource[indexPath])

}

.subscribe(onNext: {(indexPath, item)in

//self.showAlter(item: item)

})

.addDisposableTo(disposeBag)

}

functableView(_tableView:UITableView, heightForRowAt indexPath:IndexPath) ->CGFloat{

return60

}

funcscrollViewWillBeginDragging(_scrollView:UIScrollView) {

view.endEditing(true)

}

}

//MARK:上拉加载,下拉刷新

extensionTestViewController{

//添加上拉加载下拉刷新

funcrx_addMoreAndRefresh(){

//下拉刷新

header.setRefreshingTarget(self, refreshingAction:#selector(TestViewController.headerRefresh))

self.tableView.mj_header=header;

//上拉加载

footer.setRefreshingTarget(self, refreshingAction:#selector(TestViewController.footerLoadMore))

self.tableView.mj_footer=footer;

}

//下拉刷新

funcheaderRefresh()  {

page=1//赋值为1不是page == 1

rx_requestAllViewControllerData()

//rx_setupRxDataSourcesForTestViewController()

//结束刷新

self.tableView.mj_header.endRefreshing()

}

//上拉加载

funcfooterLoadMore() {

page+=1

rx_requestAllViewControllerData()

//rx_setupRxDataSourcesForTestViewController()

//结束加载

self.tableView.mj_footer.endRefreshing()

}

}

你可能感兴趣的:(RxDataSources 上拉刷新下拉加载)