0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RxSwift で Reactive

Last updated at Posted at 2018-06-11

Reactive とは

変更を監視し続けられる的な感じ?

今まで、delegateや、何やらでやっていた処理を Reactiveでは違う感じでよろしくやってくれます。

公式では

Rx is a generic abstraction of computation expressed through Observable interface.

you tube interview

ナイスミドルが作りました。

例えば、

UISegmentedControlでUITableViewの値を切り替える場合の処理は下記のようになります。



//
//  ViewController.swift
//  RxDemo
//
//  Created by Shichimitoucarashi on 2018/06/02.
//  Copyright © 2018年 keisuke yamagishi. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class ViewController: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!    
    @IBOutlet weak var segment: UISegmentedControl!

    var items = Variable(["DEDE","LOL","LPL","DIE"])

    var disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        
        super.viewDidLoad()

        let result = self.segment.rx.selectedSegmentIndex.bind { (val) in
            if Int(val) == 0 {
                self.items.value = ["DEDE","LOL","LPL","DIE"]
            }else{
                self.items.value = ["SWS","SIS","SPS","LPOP","QAQ"]
            }
        }

        self.items
            .asObservable()
            .bind(to: tableView.rx.items(cellIdentifier: "Cell")) {row,element,cell in
            
                cell.textLabel?.text = element
                
        }.addDisposableTo(self.disposableBag)
        

    }
}

このような感じになります。

tableViewに表示させてる、 items 変数の値を更新するだけで、切り替わります。

Reactive!

Reactive!

Reactive!

Reactive!

こんな感じです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?