//
// MessageSubscribePageVC.swift
// CountryRadioApp
//
// Created by ws on 2021/8/31.
//
import UIKit
import MJRefresh
import RxDataSources
fileprivate let MessageSubscriTableCellID = "MessageSubscriTableCellID"
class MessageSubscribePageVC: WSBaseViewController {
let disponseBag = DisposeBag()
let viewModel = MessageSubscirViewModel()
// let dataArray = Variable([SectionModel<String, MessageSubiceModel>]())
// var oldArr:[MessageSubiceModel] = []
var dataArray:[MessageSubiceModel] = []
lazy var tableView : UITableView = { [weak self] in
let tab = UITableView(frame: CGRect(x: 0, y: kNavBarH, width: kScreenW, height: kScreenH-kNavBarH-kBottomSafeH), style: .plain)
tab.separatorStyle = .none
tab.keyboardDismissMode = .onDrag
#if swift(>=4.2)
tab.rowHeight = UITableView.automaticDimension
#else
tab.rowHeight = UITableViewAutomaticDimension
#endif
tab.rowHeight = UITableView.automaticDimension
tab.estimatedRowHeight = 65
tab.delegate = self
tab.dataSource = self
tab.register(MessageSubscriTableCell.self, forCellReuseIdentifier: MessageSubscriTableCellID)
return tab
}()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "消息订阅"
addSubViews()
}
func addSubViews(){
view.addSubview(tableView)
// tableView.rx.setDelegate(self)
tableView.mj_header = MJRefreshStateHeader(refreshingBlock: { [weak self] in
self?.viewModel.reloadData(completed: { [weak self] (data) in
// self?.oldArr = data
// self?.dataArray.value = [SectionModel(model: "", items: data)]
self?.dataArray = data
self?.tableView.reloadData()
})
})
tableView.mj_footer = MJRefreshBackNormalFooter(refreshingBlock: { [weak self] in
self?.viewModel.loadMoreData(completed: { [weak self](data) in
// self?.dataArray.value = [SectionModel(model: "", items: self!.oldArr + data)]
self?.dataArray += data
self?.tableView.reloadData()
})
})
// tableView.mj_header?.beginRefreshing()
//获取数据
viewModel.fetch(completed: { [weak self] (arr) in
// self?.dataArray.value = [SectionModel(model: "", items: arr)]
self?.dataArray = arr
self?.tableView.reloadData()
})
// let dataSource = RxTableViewSectionedReloadDataSource<SectionModel<String,MessageSubiceModel>> { [self] (_, tableView, indexPath, model) -> UITableViewCell in
//
// guard let cell = tableView.dequeueReusableCell(withIdentifier: MessageSubscriTableCellID, for: indexPath) as? MessageSubscriTableCell else{
// fatalError("unexpected cell in collection view")
// }
// var tmpModel = model
// cell.subButton.rx.tap.subscribe { [weak self] (evetn) in
//
// var param:[String:Any] = [:]
// param["id"] = model.id
//
// if tmpModel.issubscribe == "0" { //未订阅
// RequestManager<BaseModel>().request(target: ApiManager.AddNotice(param)) { [weak self] (respone) in
//
// if respone.code == 1 {
// tmpModel.issubscribe = "1"
//
// }
// self?.view.makeToast(respone.message!)
// }
// }else{ //已订阅
// RequestManager<BaseModel>().request(target: ApiManager.CancelNotice(param)) { (respone) in
// if respone.code == 1 {
// tmpModel.issubscribe = "0"
// DispatchQueue.main.async {
// self?.tableView.reloadRows(at: [indexPath], with: .automatic)
// }
// }
// self?.view.makeToast(respone.message!)
// }
// }
//
// }.disposed(by: self.disponseBag)
//
// cell.moreButton.rx.tap.subscribe { [weak self] (event) in
//
// self?.navigationController?.pushViewController(SubsvillageListVC(), animated: true)
// XLLog("更多")
// }.disposed(by: self.disponseBag)
//
// cell.setModel(model: tmpModel)
//
// return cell
// }
//绑定数据
// self.dataArray.asObservable().bind(to: tableView.rx.items(dataSource: dataSource)).disposed(by: disponseBag)
//改变刷新状态
viewModel.refreshStatus.asObservable().subscribe(onNext: { (status) in
self.refreshStatus(status: status,tableView: self.tableView)
}).disposed(by: disponseBag)
}
/**
设置刷新状态
*/
override func refreshStatus(status:RefreshStatus,tableView: UITableView) {
switch status {
case .InvalidData: // 无效的数据
tableView.mj_header?.endRefreshing()
tableView.mj_footer?.endRefreshing()
return
case .DropDownSuccess: // 下拉成功
tableView.mj_header?.endRefreshing()
tableView.mj_footer?.endRefreshing()
case .PullSuccessHasMoreData: // 上拉,还有更多数据
tableView.mj_footer?.endRefreshing()
case .PullSuccessNoMoreData: // 上拉,没有更多数据
tableView.mj_footer?.endRefreshingWithNoMoreData()
}
tableView.mj_header?.endRefreshing()
}
}
extension MessageSubscribePageVC : UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: MessageSubscriTableCellID, for: indexPath) as? MessageSubscriTableCell else{
fatalError("unexpected cell in collection view")
}
var model = self.dataArray[indexPath.row] as? MessageSubiceModel
cell.subButton.rx.tap.subscribe { [weak self] (evetn) in
var param:[String:Any] = [:]
param["id"] = model?.id
if model?.issubscribe == "0" { //未订阅
RequestManager<BaseModel>().request(target: ApiManager.AddNotice(param)) { [weak self] (respone) in
if respone.code == 1 {
model?.issubscribe = "1"
self?.dataArray[indexPath.row] = model!
DispatchQueue.main.async {
self?.tableView.reloadRows(at: [indexPath], with: .none)
}
}
self?.view.makeToast(respone.message!)
}
}else{ //已订阅
RequestManager<BaseModel>().request(target: ApiManager.CancelNotice(param)) { (respone) in
if respone.code == 1 {
model?.issubscribe = "0"
self?.dataArray[indexPath.row] = model!
DispatchQueue.main.async {
self?.tableView.reloadRows(at: [indexPath], with: .none)
}
}
self?.view.makeToast(respone.message!)
}
}
}
cell.setModel(model: model!)
return cell
}
}
2021-09-22
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 2021年9月22日 星期三 晴 开学至今已有20多天,接下来学校工作较重要的常规工作就...
- 三是坚决打好三大攻坚战,主要目标任务如期完成。 正:We made decisive progress in th...