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.監(jiān)聽傳值
varrx_sex :Variable =Variable("")
varsexString :String{//改變sexString的值, rx_sex監(jiān)聽的值會(huì)發(fā)生改變
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
}
}