RxSwift只是基于swift语言的Rx标准实现接口库,RxSwift不包含任何Cocoa或者UI方面的类
RxCocoa:是基于RxSwift针对IOS开发的一个库,它通过Extension的方法给原生UI控件增加R x特性,使得我们更容易订阅和响应这些控件的事件。
简单建立一个TableView
-
//
-
// ViewController.swift
-
// LeanRxSwift
-
//
-
// Created by maochengfang on 2021/4/27.
-
//
-
-
import UIKit
-
import RxSwift
-
-
import Foundation
-
struct Music {
-
let name:
String;
-
let singer:
String;
-
init(name:
String,singer:
String) {
-
self.name = name
-
self.singer = singer
-
}
-
}
-
-
extension Music : CustomStringConvertible{
-
var description:
String {
-
return
"name \(name) singer \(singer)"
-
}
-
}
-
-
struct MusicDataModel {
-
let data = [
-
Music(name:
"光年之外", singer:
"摩登兄弟"),
-
-
Music(name:
"光年之外1", singer:
"摩登兄弟1"),
-
Music(name:
"光年之外2", singer:
"摩登兄弟2"),
-
Music(name:
"光年之外3", singer:
"摩登兄弟3"),
-
Music(name:
"光年之外4", singer:
"摩登兄弟4"),
-
Music(name:
"光年之外5", singer:
"摩登兄弟5"),
-
-
]
-
-
}
-
-
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
-
-
let musicDataModel =
MusicDataModel()
-
var tableView:
UITableView!
-
-
let cellID =
"testCellID"
-
-
override
func viewDidLoad() {
-
super.viewDidLoad()
-
// Do any additional setup after loading the view.
-
self.tableView =
UITableView(frame:
CGRect(x:
0,y:
0,width:
self.view.frame.size.width,height:
self.view.frame.size.height), style:
UITableView.
Style.plain)
-
self.tableView.delegate =
self;
-
self.tableView.dataSource =
self;
-
self.tableView.rowHeight =
50;
-
self.tableView.register(
UITableViewCell.classForCoder() , forCellReuseIdentifier: cellID)
-
self.view.addSubview(
self.tableView)
-
}
-
-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) ->
Int {
-
return musicDataModel.data.
count
-
}
-
-
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
UITableViewCell {
-
let cell = tableView.dequeueReusableCell(withIdentifier: cellID,
for: indexPath)
-
let music = musicDataModel.data[indexPath.row]
-
cell.textLabel?.text = music.name
-
cell.detailTextLabel?.text = music.singer
-
return cell
-
-
}
-
-
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
-
print(
"你选中的歌曲信息[\(musicDataModel.data[indexPath.row])]")
-
}
-
-
}
二、进行RX改造
001 将data的属性变成一个可观察序列对象,而对象中的内容不变
-
//
-
// ViewController.swift
-
// LeanRxSwift
-
//
-
// Created by maochengfang on 2021/4/27.
-
//
-
-
import UIKit
-
import RxSwift
-
import RxCocoa
-
-
import Foundation
-
struct Music {
-
let name:
String;
-
let singer:
String;
-
init(name:
String,singer:
String) {
-
self.name = name
-
self.singer = singer
-
}
-
}
-
-
extension Music : CustomStringConvertible{
-
var description:
String {
-
return
"name \(name) singer \(singer)"
-
}
-
}
-
-
struct MusicDataModel {
-
let data =
Observable.just([
-
Music(name:
"光年之外", singer:
"摩登兄弟"),
-
-
Music(name:
"光年之外1", singer:
"摩登兄弟1"),
-
Music(name:
"光年之外2", singer:
"摩登兄弟2"),
-
Music(name:
"光年之外3", singer:
"摩登兄弟3"),
-
Music(name:
"光年之外4", singer:
"摩登兄弟4"),
-
Music(name:
"光年之外5", singer:
"摩登兄弟5"),
-
-
])
-
-
}
-
-
class ViewController: UIViewController {
-
-
let musicDataModel =
MusicDataModel()
-
var tableView:
UITableView!
-
-
-
let cellID =
"testCellID"
-
let disposeBag =
DisposeBag()
-
-
override
func viewDidLoad() {
-
super.viewDidLoad()
-
// Do any additional setup after loading the view.
-
self.tableView =
UITableView(frame:
CGRect(x:
0,y:
0,width:
self.view.frame.size.width,height:
self.view.frame.size.height), style:
UITableView.
Style.plain)
-
self.tableView.rowHeight =
50;
-
self.tableView.register(
UITableViewCell.classForCoder() , forCellReuseIdentifier: cellID)
-
self.view.addSubview(
self.tableView)
-
-
musicDataModel.data.bind(to: tableView.rx.items(cellIdentifier: cellID)){
-
_,music, cell
in cell.textLabel?.text = music.name
-
cell.detailTextLabel?.text = music.singer
-
}.disposed(by: disposeBag)
-
-
tableView.rx.modelSelected(
Music.
self).subscribe(onNext: {music
in
print(
"你选中的歌曲信息\(music)")}).disposed(by: disposeBag)
-
-
}
-
-
// func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
-
// return musicDataModel.data.count
-
// }
-
-
// func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
-
// let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
-
// let music = musicDataModel.data[indexPath.row]
-
// cell.textLabel?.text = music.name
-
// cell.detailTextLabel?.text = music.singer
-
// return cell
-
//
-
// }
-
//
-
// func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
-
// print("你选中的歌曲信息[\(musicDataModel.data[indexPath.row])]")
-
// }
-
-
}
DisposeBag:的作用是在Rx在视图控制器或者其持有者将要销毁的生活自动释放掉绑定它上面的资源,它是通过类似于订阅处理机制等方式实现
tableView.rx.items 这是Rx基于cellForRowAt数据源的封装,传统方式中还有一个numberOfRowsInSection方法,使用RX后就不需要了
tableView.rx.modelSelected 这是Rx基于didSelectRowAt的一个封装。
传统方式必须实现很多行代码,经过Rx改造之后,代码量会减少很多
转载:https://blog.csdn.net/Coding_Physical/article/details/116211678
查看评论