RxSwift和如何使简单的TableViewController?

时间:2015-12-05 23:54:31

标签: swift rx-swift

如何创建RxSwift风格的TableViewController?

我正在尝试创建一个使用RxSwift的简单TableViewController,并且没有任何部分。

我已经广泛地看了https://github.com/ReactiveX/RxSwift/blob/master/RxExample/RxExample/Examples/TableView/TableViewController.swift

我将代码仅限于一个部分,仅使用用户。但是,似乎我仍然坚持使用SectionModel。

//
//  TableViewController.swift
//  RxExample
//
//  Created by carlos on 26/5/15.
//  Copyright (c) 2015 Krunoslav Zaher. All rights reserved.
//
// modified by Mike Finney for a StackOverflow question

import UIKit
#if !RX_NO_MODULE
import RxSwift
import RxCocoa
#endif

class TableViewController: ViewController, UITableViewDelegate {


    @IBOutlet weak var tableView: UITableView!

    var disposeBag = DisposeBag()

    let users = Variable([User]())

    let dataSource = RxTableViewSectionedReloadDataSource<SectionModel<String, User>>()

    typealias Section = SectionModel<String, User>

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.rightBarButtonItem = self.editButtonItem()

        users
            .map { [ SectionModel(model: "ok", items: $0) ] }
            .bindTo(tableView.rx_itemsWithDataSource(dataSource))
            .addDisposableTo(disposeBag)

        dataSource.cellFactory = { (tv, ip, user: User) in
            let cell = tv.dequeueReusableCellWithIdentifier("Cell")!
            cell.textLabel?.text = user.firstName + " " + user.lastName
            return cell
        }

        // customization using delegate
        // RxTableViewDelegateBridge will forward correct messages
        tableView.rx_setDelegate(self)
            .addDisposableTo(disposeBag)

        tableView.rx_itemSelected
            .subscribeNext { [unowned self] indexPath in
                self.showDetailsForUserAtIndexPath(indexPath)
            }
            .addDisposableTo(disposeBag)

        tableView.rx_itemDeleted
            .subscribeNext { [unowned self] indexPath in
                self.removeUser(indexPath)
            }
            .addDisposableTo(disposeBag)

        RandomUserAPI.sharedAPI.getExampleUserResultSet()
            .subscribeNext { [unowned self] array in
                self.users.value = array
            }
            .addDisposableTo(disposeBag)

    }

    override func setEditing(editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        tableView.editing = editing
    }

    // MARK: Table view delegate ;)

    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }

    // MARK: Navigation

    private func showDetailsForUserAtIndexPath(indexPath: NSIndexPath) {
        let sb = UIStoryboard(name: "Main", bundle: NSBundle(identifier: "RxExample-iOS"))
        let vc = sb.instantiateViewControllerWithIdentifier("DetailViewController") as! DetailViewController
        vc.user = getUser(indexPath)
        self.navigationController?.pushViewController(vc, animated: true)
    }

    // MARK: Work over Variable

    func getUser(indexPath: NSIndexPath) -> User {
        var array: [User]
        switch indexPath.section {
        case 0:
            array = users.value
        default:
            fatalError("Section out of range")
        }
        return array[indexPath.row]
    }

    func moveUserFrom(from: NSIndexPath, to: NSIndexPath) {
        var user: User
        var fromArray: [User]
        var toArray: [User]

        fromArray = users.value
        user = fromArray.removeAtIndex(from.row)
        users.value = fromArray

        toArray = users.value
        toArray.insert(user, atIndex: to.row)
        users.value = toArray
    }

    func addUser(user: User) {
        var array = users.value
        array.append(user)
        users.value = array
    }

    func removeUser(indexPath: NSIndexPath) {
        var array: [User]
        switch indexPath.section {
        case 0:
            array = users.value
            array.removeAtIndex(indexPath.row)
            users.value = array
        default:
            fatalError("Section out of range")
        }
    }

}

如果我能提供帮助,我甚至不想使用SectionModel。

所以也许另一种要问的方式是&#34; RxTableViewSectionedReloadDataSource的非节版本是什么?&#34;

1 个答案:

答案 0 :(得分:7)

2015年12月6日左右,RxSwift / RxExample增加了一个新示例。要查看的视图控制器是SimpleTableViewExampleViewController.swift代码中的RxSwift

如果运行示例,请选择标题为“最简单的表视图示例”的示例 enter image description here