RxSwift/RxCocoa/iOS/Proxies/RxTableViewDataSourceProxy.swift
2016-04-22 20:49:18 +02:00

128 lines
3.9 KiB
Swift

//
// RxTableViewDataSourceProxy.swift
// RxCocoa
//
// Created by Krunoslav Zaher on 6/15/15.
// Copyright © 2015 Krunoslav Zaher. All rights reserved.
//
#if os(iOS) || os(tvOS)
import Foundation
import UIKit
#if !RX_NO_MODULE
import RxSwift
#endif
let tableViewDataSourceNotSet = TableViewDataSourceNotSet()
class TableViewDataSourceNotSet
: NSObject
, UITableViewDataSource {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
rxAbstractMethodWithMessage(dataSourceNotSet)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
rxAbstractMethodWithMessage(dataSourceNotSet)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
rxAbstractMethodWithMessage(dataSourceNotSet)
}
}
/**
For more information take a look at `DelegateProxyType`.
*/
public class RxTableViewDataSourceProxy
: DelegateProxy
, UITableViewDataSource
, DelegateProxyType {
/**
Typed parent object.
*/
public weak private(set) var tableView: UITableView?
private weak var _requiredMethodsDataSource: UITableViewDataSource? = tableViewDataSourceNotSet
/**
Initializes `RxTableViewDataSourceProxy`
- parameter parentObject: Parent object for delegate proxy.
*/
public required init(parentObject: AnyObject) {
self.tableView = (parentObject as! UITableView)
super.init(parentObject: parentObject)
}
// MARK: delegate
/**
Required delegate method implementation.
*/
public func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return (_requiredMethodsDataSource ?? tableViewDataSourceNotSet).numberOfSectionsInTableView?(tableView) ?? 1
}
/**
Required delegate method implementation.
*/
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (_requiredMethodsDataSource ?? tableViewDataSourceNotSet).tableView(tableView, numberOfRowsInSection: section)
}
/**
Required delegate method implementation.
*/
public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return (_requiredMethodsDataSource ?? tableViewDataSourceNotSet).tableView(tableView, cellForRowAt: indexPath)
}
// MARK: proxy
/**
For more information take a look at `DelegateProxyType`.
*/
public override class func createProxyForObject(object: AnyObject) -> AnyObject {
let tableView = (object as! UITableView)
return castOrFatalError(tableView.rx_createDataSourceProxy())
}
/**
For more information take a look at `DelegateProxyType`.
*/
public override class func delegateAssociatedObjectTag() -> UnsafePointer<Void> {
return _pointer(p: &dataSourceAssociatedTag)
}
/**
For more information take a look at `DelegateProxyType`.
*/
public class func setCurrentDelegate(delegate: AnyObject?, toObject object: AnyObject) {
let tableView: UITableView = castOrFatalError(object)
tableView.dataSource = castOptionalOrFatalError(delegate)
}
/**
For more information take a look at `DelegateProxyType`.
*/
public class func currentDelegateFor(object: AnyObject) -> AnyObject? {
let tableView: UITableView = castOrFatalError(object)
return tableView.dataSource
}
/**
For more information take a look at `DelegateProxyType`.
*/
public override func setForwardToDelegate(forwardToDelegate: AnyObject?, retainDelegate: Bool) {
let requiredMethodsDataSource: UITableViewDataSource? = castOptionalOrFatalError(forwardToDelegate)
_requiredMethodsDataSource = requiredMethodsDataSource ?? tableViewDataSourceNotSet
super.setForwardToDelegate(forwardToDelegate: forwardToDelegate, retainDelegate: retainDelegate)
}
}
#endif