学习RXSwift之传统和Rx改在对比

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改造之后,代码量会减少很多

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值