Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds new Expansion style #54

Merged
merged 3 commits into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions ExpandableCell/ExpandableProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ class ExpandableProcessor {
expandableDatasPerSection[indexPath.section] = expandableDatas
}

func deleteAllIndexPathsInSection(_ section: Int) -> (expandedIndexPaths:[IndexPath], indexPaths: [IndexPath]) {
var expandedIndexPaths = [IndexPath]()
var indexPaths = [IndexPath]()

if let expandableDataInSection = expandableDatasPerSection[section] {
for expandableData in expandableDataInSection {
indexPaths.append(expandableData.indexPath)
for expandedIndexPath in expandableData.expandedIndexPaths {
expandedIndexPaths.append(expandedIndexPath)
}
}
}

expandableDatasPerSection.removeValue(forKey: section)

return (expandedIndexPaths, indexPaths)
}

func deleteAllIndexPaths() -> (expandedIndexPaths:[IndexPath], indexPaths: [IndexPath]) {
var expandedIndexPaths = [IndexPath]()
var indexPaths = [IndexPath]()
Expand Down
30 changes: 28 additions & 2 deletions ExpandableCell/ExpandableTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extension ExpandableTableView {
public enum ExpansionStyle {
case multi
case single
case singlePerSection
}
}

Expand All @@ -52,14 +53,20 @@ extension ExpandableTableView: UITableViewDataSource, UITableViewDelegate {
let expandedData = expandableProcessor.isExpandedCell(at: indexPath)
if !expandedData.isExpandedCell {
delegate.expandableTableView(self, didSelectRowAt: indexPath)

if expandableProcessor.isExpandable(at: indexPath) {
// Check if there are other rows expanded in section
let noExpandedRowsInSection = expandableProcessor.numberOfExpandedRowsInSection(section: indexPath.section) == 0

if let cell = self.cellForRow(at: indexPath) {
if self.expansionStyle == .single {
closeAll()
} else if self.expansionStyle == .singlePerSection && !noExpandedRowsInSection {
closeAllInSection(indexPath.section)
}
if let correctIndexPath = self.indexPath(for: cell) {
// Check if there are other rows expanded in section
if expandableProcessor.numberOfExpandedRowsInSection(section: correctIndexPath.section) == 0 {
// If no other row is expanded in section
if noExpandedRowsInSection {
let originalIndexPath = expandableProcessor.original(indexPath: correctIndexPath)
open(indexPath: originalIndexPath, delegate: delegate)
} else {
Expand Down Expand Up @@ -250,6 +257,25 @@ extension ExpandableTableView {
_ = closeAllIndexPaths()
}

public func closeAllInSection(_ section: Int) {
_ = closeAllIndexPathsInSection(section)
}

public func closeAllIndexPathsInSection(_ section: Int) -> [IndexPath] {
let allIndexPaths = expandableProcessor.deleteAllIndexPathsInSection(section)
let expandedIndexPaths = allIndexPaths.expandedIndexPaths
let indexPaths = allIndexPaths.indexPaths

for indexPath in indexPaths {
if let cell = self.cellForRow(at: indexPath) as? ExpandableCell {
cell.close()
}
}

self.deleteRows(at: expandedIndexPaths, with: animation)
return indexPaths
}

public func close(at indexPath: IndexPath) {
guard let cell = self.cellForRow(at: indexPath) as? ExpandableCell else { return }
if cell.isExpanded() {
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Set required `ExpandableDelegate` method.
| Property | Type | Explanation |
| -------- | ---- | ----------- |
| `animation` | `UITableViewRowAnimation` | Animation when open and close |
| `expansionStyle` | `ExpandableTableView.ExpansionStyle` | Select expansion type: single: One row at a time, multi: Any number of rows at a time|
| `expansionStyle` | `ExpandableTableView.ExpansionStyle` | Select expansion type:<br>**single** - one row at a time;<br>**singlePerSection** - one row at a time, per section;<br>**multi** - any number of rows at a time|

#### ExpandableTableView methods
| Method | Explanation |
Expand Down