lazy var sexViewModel:SexViewModel= {
let sexViewModel =SexViewModel()
return sexViewModel
}()
overridefuncviewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
title="性别"
view.addSubview(tableView)
tableView.dataSource= (sexViewModelasUITableViewDataSource)
tableView.delegate= (sexViewModelasUITableViewDelegate)
//接收传值
sexViewModel.rx_sex.asObservable().subscribe(onNext: {[weakself] (sexString)in
printLog(message:"接收性别--\(sexString)")
if(sexString.isEmpty){
return
}
self?.navigationController?.popViewController(animated:true)
}).addDisposableTo(disposeBag)
}
class SexViewModel:NSObject{
lazy var titleArray :[String] = {
let titleArray = ["男","女"]
return titleArray
}()
//1.保存选中行
varmarkSelectCellRow =0
//2.监听传值
varrx_sex :Variable =Variable("")
varsexString :String{//改变sexString的值, rx_sex监听的值会发生改变
get{//得到旧值
return rx_sex.value
}
set{//将旧值换成新值
printLog(message:"将旧值换成新值\(rx_sex.value)")
return rx_sex.value= newValue
}
}
overrideinit() {
}
}
//MARK:UITableViewDelegate
extensionSexViewModel:UITableViewDataSource,UITableViewDelegate{
functableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int{
return2
}
functableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell{
letcell = tableView.dequeueReusableCell(withIdentifier:"sexViewTableViewCell", for: indexPath) as! SexViewTableViewCell
cell.selectionStyle= .none
cell.titleLabel.text=titleArray[indexPath.row]
cell.rightImageView.isHidden= (indexPath.row==markSelectCellRow) ?false:true
return cell
}
functableView(_tableView:UITableView, didSelectRowAt indexPath:IndexPath) {
markSelectCellRow= indexPath.row
tableView.reloadData()
//改变sexString的值,传值
sexString=titleArray[indexPath.row]
}
functableView(_tableView:UITableView, heightForRowAt indexPath:IndexPath) ->CGFloat{
return50
}
}