Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
Minor swift improvements. Added dynamic count capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Prince2k3 committed Feb 20, 2019
1 parent 2485731 commit 624336a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
16 changes: 9 additions & 7 deletions DataSource/Sources/DataSource+UICollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,27 @@ 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
}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
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
}

Expand Down
28 changes: 12 additions & 16 deletions DataSource/Sources/DataSource+UITableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,19 @@ 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
}

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
}
Expand 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
}
Expand 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
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions DataSource/Sources/DataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 624336a

Please sign in to comment.