// ViewController.swift
// RxSwiftTest
//
// Created by travey on 2018/11/5.
// Copyright © 2018年 ZhouShijie. All rights reserved.
import UIKit
import RxSwift
import RxCocoa
import SnapKit
import RxDataSources
class ViewController: UIViewController {
let disposeBag = DisposeBag()
var tableView: UITableView!
override func viewDidLoad() {
tableView = UITableView(frame: view.frame, style: .plain)
tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
view.addSubview(tableView)
let items = Observable.just([
"C++",
"C",
"Swift",
"Go",
"Java"
])
items.bind(to: tableView.rx.items) { (tableView, row, element) in
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text = "\(row): \(element)"
return cell!
}.disposed(by: disposeBag)
// 输出选中项的索引
tableView.rx.itemSelected
.subscribe(onNext: { indexPath in
print("这是第\(indexPath.row)行")
}).disposed(by: disposeBag)
// 输出选中项的内容
tableView.rx.modelSelected(String.self)
.subscribe(onNext: { item in
print("选中的内容为\(item)")
}).disposed(by: disposeBag)
}
}