 
 					if let indexSet = changes.deletedSections {
 						tableView.deleteSections(NSMutableIndexSet(converting: indexSet),
-							withRowAnimation: self.configuration.rowAnimation)
+							withRowAnimation: self.rowAnimation)
 					}
 
 					if let indexSet = changes.reloadedSections {
 						tableView.reloadSections(NSMutableIndexSet(converting: indexSet),
-							withRowAnimation: self.configuration.rowAnimation)
+							withRowAnimation: self.rowAnimation)
 					}
 
 					if let indexPaths = changes.deletedRows {
 						tableView.deleteRowsAtIndexPaths(indexPaths.map { NSIndexPath(converting: $0) },
-							withRowAnimation: self.configuration.rowAnimation)
+							withRowAnimation: self.rowAnimation)
 					}
 
-					if self.configuration.shouldReloadRowsForUpdatedObjects, let indexPaths = changes.updatedRows {
+					if self.shouldReloadRowsForUpdatedObjects, let indexPaths = changes.updatedRows {
 						tableView.reloadRowsAtIndexPaths(indexPaths.map { NSIndexPath(converting: $0) },
-							withRowAnimation: self.configuration.rowAnimation)
+							withRowAnimation: self.rowAnimation)
 					}
 
 					if let indexSet = changes.insertedSections {
 						tableView.insertSections(NSMutableIndexSet(converting: indexSet),
-							withRowAnimation: self.configuration.rowAnimation)
+							withRowAnimation: self.rowAnimation)
 					}
 
 					if let indexPathPairs = changes.movedRows {

import UIKit
import ReactiveCocoa

public struct TableViewAdapterConfiguration<V: ViewModel> {
	private var factories: [Int: (UITableView, V, NSIndexPath) -> UITableViewCell]
	public var isUniform = false
	public var shouldReloadRowsForUpdatedObjects = false
	public var rowAnimation: UITableViewRowAnimation = .Automatic
	public var sectionNameTransform: ((Int, String?) -> String?)?
	public var emptySetHandler: (Bool -> Void)?

	public init() {
		self.factories = [:]
	}

	public mutating func registerForAllSection(factory: (UITableView, V, NSIndexPath) -> UITableViewCell) {
		isUniform = true
		factories[-1] = factory
	}

	public mutating func registerSection(index: Int, factory: (UITableView, V, NSIndexPath) -> UITableViewCell) {
		precondition(index >= 0, "section index must be greater than or equal to zero.")
		factories[index] = factory
	}

	public func apply(indexPath: NSIndexPath, tableView: UITableView, viewModel: V) -> UITableViewCell {
		if isUniform {
			return factories[-1]!(tableView, viewModel, indexPath)
		}
		return factories[indexPath.section]!(tableView, viewModel, indexPath)
	}
}

final public class TableViewAdapter<V: ViewModel>: NSObject, UITableViewDataSource {
	private let set: ViewModelSet<V>
	private let configuration: TableViewAdapterConfiguration<V>
	private var isEmpty: Bool = true

	public init(set: ViewModelSet<V>, configuration: TableViewAdapterConfiguration<V>) {
		self.set = set
		self.configuration = configuration
	}

	public func bind(tableView: UITableView) {
		tableView.dataSource = self
		set.eventProducer
			.takeUntil(tableView.willDeinitProducer)
			.takeUntil(willDeinitProducer)
			.startWithNext { [unowned self, unowned tableView] in
				switch($0) {
				case .reloaded:
					tableView.reloadData()


					if let indexPaths = changes.insertedRows {
						tableView.insertRowsAtIndexPaths(indexPaths.map { NSIndexPath(converting: $0) },
							withRowAnimation: self.rowAnimation)
					}

					tableView.endUpdates()
