RxSwift 以及如何制作简单的 TableViewController?

RxSwift and How To Make Simple TableViewController?

如何创建 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 Whosebug 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: [=10=]) ] }
            .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。

所以也许另一种提问方式是 "What is the non-section version of RxTableViewSectionedReloadDataSource?"

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

如果您 运行 示例,select 标题为 "Simplest table view example" 的示例