Skip to content

Commit

Permalink
Handle potential table/collection view binding issue
Browse files Browse the repository at this point in the history
  • Loading branch information
srdanrasic committed Nov 29, 2015
1 parent 4004ade commit 9cca67c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
4 changes: 2 additions & 2 deletions ReactiveUIKit.podspec
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Pod::Spec.new do |s|
s.name = "ReactiveUIKit"
s.version = "1.0.6"
s.version = "1.0.8"
s.summary = "Reactive extensions for UIKit framework."
s.homepage = "https://github.com/ReactiveKit/ReactiveUIKit"
s.license = 'MIT'
s.author = { "Srdan Rasic" => "[email protected]" }
s.source = { :git => "https://github.com/ReactiveKit/ReactiveUIKit.git", :tag => "v1.0.6" }
s.source = { :git => "https://github.com/ReactiveKit/ReactiveUIKit.git", :tag => "v1.0.8" }

s.ios.deployment_target = '8.0'
s.tvos.deployment_target = '9.0'
Expand Down
2 changes: 1 addition & 1 deletion ReactiveUIKit/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0.6</string>
<string>1.0.8</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
13 changes: 8 additions & 5 deletions ReactiveUIKit/UICollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ extension ObservableCollectionType where Collection.Index == Int {

public class RKCollectionViewDataSource<C: ObservableCollectionType where C.Collection.Index == Int>: NSObject, UICollectionViewDataSource {

private let collection: C
private let observableCollection: C
private var sourceCollection: C.Collection
private weak var collectionView: UICollectionView!
private let createCell: (NSIndexPath, C.Collection, UICollectionView) -> UICollectionViewCell
private weak var proxyDataSource: RKCollectionViewProxyDataSource?
Expand All @@ -64,15 +65,17 @@ public class RKCollectionViewDataSource<C: ObservableCollectionType where C.Coll
self.collectionView = collectionView
self.createCell = createCell
self.proxyDataSource = proxyDataSource
self.collection = collection
self.observableCollection = collection
self.sourceCollection = collection.collection
self.animated = animated
super.init()

collectionView.dataSource = self
collectionView.reloadData()

collection.observe(on: ImmediateOnMainExecutionContext) { [weak self] event in
observableCollection.observe(on: ImmediateOnMainExecutionContext) { [weak self] event in
if let uSelf = self {
uSelf.sourceCollection = event.collection
if animated {
uSelf.collectionView.performBatchUpdates({
RKCollectionViewDataSource.applyRowUnitChangeSet(event, collectionView: uSelf.collectionView, sectionIndex: 0, dataSource: uSelf.proxyDataSource)
Expand Down Expand Up @@ -109,11 +112,11 @@ public class RKCollectionViewDataSource<C: ObservableCollectionType where C.Coll
}

@objc public func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return collection.collection.count
return sourceCollection.count
}

@objc public func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
return createCell(indexPath, collection.collection, collectionView)
return createCell(indexPath, sourceCollection, collectionView)
}

@objc public func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
Expand Down
15 changes: 9 additions & 6 deletions ReactiveUIKit/UITableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ extension ObservableCollectionType where Collection.Index == Int {

public class RKTableViewDataSource<C: ObservableCollectionType where C.Collection.Index == Int>: NSObject, UITableViewDataSource {

private let collection: C
private let observableCollection: C
private var sourceCollection: C.Collection
private weak var tableView: UITableView!
private let createCell: (NSIndexPath, C.Collection, UITableView) -> UITableViewCell
private weak var proxyDataSource: RKTableViewProxyDataSource?
Expand All @@ -75,15 +76,17 @@ public class RKTableViewDataSource<C: ObservableCollectionType where C.Collectio
self.tableView = tableView
self.createCell = createCell
self.proxyDataSource = proxyDataSource
self.collection = collection
self.observableCollection = collection
self.sourceCollection = collection.collection
self.animated = animated
super.init()

tableView.dataSource = self
tableView.reloadData()

collection.observe(on: ImmediateOnMainExecutionContext) { [weak self] event in
observableCollection.observe(on: ImmediateOnMainExecutionContext) { [weak self] event in
if let uSelf = self {
uSelf.sourceCollection = event.collection
if animated {
uSelf.tableView.beginUpdates()
RKTableViewDataSource.applyRowUnitChangeSet(event, tableView: uSelf.tableView, sectionIndex: 0, dataSource: uSelf.proxyDataSource)
Expand Down Expand Up @@ -120,11 +123,11 @@ public class RKTableViewDataSource<C: ObservableCollectionType where C.Collectio
}

@objc public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return collection.collection.count
return sourceCollection.count
}

@objc public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return createCell(indexPath, collection.collection, tableView)
return createCell(indexPath, sourceCollection, tableView)
}

@objc public func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
Expand Down

0 comments on commit 9cca67c

Please sign in to comment.