From 624336a406ea2a13fe6d913f4ca4d9095079233e Mon Sep 17 00:00:00 2001 From: Prince Ugwuh Date: Wed, 20 Feb 2019 16:15:23 -0600 Subject: [PATCH] Minor swift improvements. Added dynamic count capabilities --- .../Sources/DataSource+UICollectionView.swift | 16 ++++++----- .../Sources/DataSource+UITableView.swift | 28 ++++++++----------- DataSource/Sources/DataSource.swift | 5 ++-- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/DataSource/Sources/DataSource+UICollectionView.swift b/DataSource/Sources/DataSource+UICollectionView.swift index c022ad7..83d503f 100644 --- a/DataSource/Sources/DataSource+UICollectionView.swift +++ b/DataSource/Sources/DataSource+UICollectionView.swift @@ -32,12 +32,11 @@ extension DataSource: UICollectionViewDataSource { public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { if !self.staticItems.isEmpty { return self.staticItems.count + } else if let numberOfItems = self.numberOfItems { + return numberOfItems + } else if !self.items.isEmpty { + return self.loadingMoreCellIdentifier != nil ? self.items.count + 1 : self.items.count } - - if !self.items.isEmpty { - return self.items.count - } - return 0 } @@ -45,12 +44,15 @@ extension DataSource: UICollectionViewDataSource { let identifier: String var item: Any? - if !self.staticItems.isEmpty { + if let loadingMoreCellIdentifier = self.loadingMoreCellIdentifier, + self.items.count == indexPath.row { + identifier = loadingMoreCellIdentifier + } else if !self.staticItems.isEmpty { let staticItem = self.staticItems[indexPath.row] identifier = staticItem.identifier item = staticItem.item } else { - item = self.item(at: indexPath) + item = self.items[indexPath.row] identifier = self.cellIdentifier } diff --git a/DataSource/Sources/DataSource+UITableView.swift b/DataSource/Sources/DataSource+UITableView.swift index 20a2acf..2d07ed4 100644 --- a/DataSource/Sources/DataSource+UITableView.swift +++ b/DataSource/Sources/DataSource+UITableView.swift @@ -32,14 +32,10 @@ extension DataSource: UITableViewDataSource { public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if !self.staticItems.isEmpty { return self.staticItems.count - } - - if !self.items.isEmpty { - if self.loadingMoreCellIdentifier != nil { - return self.items.count + 1 - } - - return self.items.count + } else if let numberOfItems = self.numberOfItems { + return numberOfItems + } else if !self.items.isEmpty { + return self.loadingMoreCellIdentifier != nil ? self.items.count + 1 : self.items.count } return 0 @@ -47,8 +43,8 @@ extension DataSource: UITableViewDataSource { public func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { if self.isMovable { - if let movableItems = self.movableItems { - return movableItems.contains(indexPath) + if !self.movableItems.isEmpty { + return self.movableItems.contains(indexPath) } else { return true // all } @@ -59,8 +55,8 @@ extension DataSource: UITableViewDataSource { public func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { if self.isEditable { - if let editableItems = self.editableItems { - return editableItems.keys.contains(indexPath) + if !self.editableItems.isEmpty { + return self.editableItems.keys.contains(indexPath) } else { return true // all } @@ -79,13 +75,13 @@ extension DataSource: UITableViewDataSource { } public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - var identifier: String? + let identifier: String var item: Any? if let loadingMoreCellIdentifier = self.loadingMoreCellIdentifier, - self.items.count == indexPath.row { + self.items.count == indexPath.row { identifier = loadingMoreCellIdentifier - } else if self.staticItems.count > 0 { + } else if !self.staticItems.isEmpty { let staticItem = self.staticItems[indexPath.row] identifier = staticItem.identifier item = staticItem.item @@ -94,7 +90,7 @@ extension DataSource: UITableViewDataSource { identifier = self.cellIdentifier } - let cell = tableView.dequeueReusableCell(withIdentifier: identifier!, for: indexPath) + let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) if let cell = cell as? DataSourceConfigurable { cell.configure(item) diff --git a/DataSource/Sources/DataSource.swift b/DataSource/Sources/DataSource.swift index c39fce4..2eede6c 100644 --- a/DataSource/Sources/DataSource.swift +++ b/DataSource/Sources/DataSource.swift @@ -54,13 +54,14 @@ public final class DataSource: NSObject { public weak var editingStyleDelegate: DataSourceEditingStyleDelegate? public var items: [Any] = [] + public var numberOfItems: Int? public var title: String? public var headerItem: DataSourceStaticItem? public var footerItem: DataSourceStaticItem? public var isEditable: Bool = false public var isMovable: Bool = false - public var editableItems: [IndexPath: NSNumber]? // NSNumber represents the UITableViewCellEditingStyle - public var movableItems: [IndexPath]? + public var editableItems: [IndexPath: UITableViewCell.EditingStyle] = [:] + public var movableItems: [IndexPath] = [] public var loadingMoreCellIdentifier: String? override init() {