From 25a4ba9cf09b01e4946db55d226b9ceb24b46679 Mon Sep 17 00:00:00 2001 From: Pavan Kataria Date: Wed, 19 Jun 2019 05:11:55 +0100 Subject: [PATCH] feat: completes fixed columns implementation --- .../MenuTableViewController.swift | 14 ++++-- .../Models/DataTableConfiguration.swift | 2 + SwiftDataTables/Classes/SwiftDataTable.swift | 4 ++ .../Classes/SwiftDataTableLayout.swift | 46 ++++++++++--------- 4 files changed, 40 insertions(+), 26 deletions(-) diff --git a/Example/DemoSwiftDataTables/MenuTableViewController.swift b/Example/DemoSwiftDataTables/MenuTableViewController.swift index 7cda5e9..cf4bd8e 100644 --- a/Example/DemoSwiftDataTables/MenuTableViewController.swift +++ b/Example/DemoSwiftDataTables/MenuTableViewController.swift @@ -166,11 +166,12 @@ extension MenuViewController { } private func createdExampleConfigurations() -> [MenuItem] { var section = [MenuItem]() - section.append(MenuItem(title: "Without Footers", config: self.configurationWithoutFooter())) - section.append(MenuItem(title: "Without Search", config: self.configurationWithoutSearch())) + section.append(MenuItem(title: "Without Footers", config: configurationWithoutFooter())) + section.append(MenuItem(title: "Without Search", config: configurationWithoutSearch())) section.append(MenuItem(title: "Without floating headers and footers", config: self.configurationWithoutFloatingHeadersAndFooters())) - section.append(MenuItem(title: "Without scroll bars", config: self.configurationWithoutScrollBars())) - section.append(MenuItem(title: "Alternating colours", config: self.configurationAlternatingColours())) + section.append(MenuItem(title: "Without scroll bars", config: configurationWithoutScrollBars())) + section.append(MenuItem(title: "Alternating colours", config: configurationAlternatingColours())) + section.append(MenuItem(title: "Fixed/Frozen columns", config: configurationFixedColumns())) return section } } @@ -223,6 +224,11 @@ extension MenuViewController { ] return configuration } + private func configurationFixedColumns() -> DataTableConfiguration { + var configuration = DataTableConfiguration() + configuration.fixedColumns = DataTableFixedColumnType(leftColumns: 1, rightColumns: 1) + return configuration + } } extension UIColor { diff --git a/SwiftDataTables/Classes/Models/DataTableConfiguration.swift b/SwiftDataTables/Classes/Models/DataTableConfiguration.swift index 27f4449..3365475 100755 --- a/SwiftDataTables/Classes/Models/DataTableConfiguration.swift +++ b/SwiftDataTables/Classes/Models/DataTableConfiguration.swift @@ -46,6 +46,8 @@ public struct DataTableConfiguration: Equatable { .white ] + public var fixedColumns: DataTableFixedColumnType? = nil + public init(){ } diff --git a/SwiftDataTables/Classes/SwiftDataTable.swift b/SwiftDataTables/Classes/SwiftDataTable.swift index 7d0f6e1..c4d3e94 100755 --- a/SwiftDataTables/Classes/SwiftDataTable.swift +++ b/SwiftDataTables/Classes/SwiftDataTable.swift @@ -711,6 +711,10 @@ extension SwiftDataTable { } return 35 } + + func fixedColumns() -> DataTableFixedColumnType? { + return delegate?.fixedColumns?(for: self) ?? self.options.fixedColumns + } } //MARK: - Search Bar Delegate diff --git a/SwiftDataTables/Classes/SwiftDataTableLayout.swift b/SwiftDataTables/Classes/SwiftDataTableLayout.swift index d62ded4..34451c4 100755 --- a/SwiftDataTables/Classes/SwiftDataTableLayout.swift +++ b/SwiftDataTables/Classes/SwiftDataTableLayout.swift @@ -128,7 +128,7 @@ class SwiftDataTableLayout: UICollectionViewFlowLayout { guard att.frame.minY <= rect.maxY else { break } attributes.append(att) } - attributes = attributes.compactMap { self.adjustItemPosition(attributes: $0) } + attributes = attributes.compactMap { self.adjustAttributesPosition($0, at: $0.indexPath.item, zIndexPosition: 1) } // //MARK: Search Header // if self.dataTable.shouldShowSearchSection(){ // let menuLengthIndexPath = IndexPath(index: 0) @@ -178,24 +178,25 @@ class SwiftDataTableLayout: UICollectionViewFlowLayout { //MARK: - Layout Attributes For Elements And Supplmentary Views extension SwiftDataTableLayout { - func adjustItemPosition(attributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes { - guard let fixedColumns = self.dataTable.delegate?.fixedColumns?(for: self.dataTable) else { + func adjustAttributesPosition(_ attributes: UICollectionViewLayoutAttributes?, at columnIndex: Int, zIndexPosition: Int) -> UICollectionViewLayoutAttributes? { + guard let attributes = attributes else { return nil } + guard let fixedColumns = self.dataTable.fixedColumns() else { return attributes } - guard let fixedColumnSide = fixedColumns.hitTest(attributes.indexPath.item, totalTableColumnCount: dataTable.numberOfColumns()) else { + guard let fixedColumnSide = fixedColumns.hitTest(columnIndex, totalTableColumnCount: dataTable.numberOfColumns()) else { return attributes } var xOffset: CGFloat = self.dataTable.collectionView.contentOffset.x switch fixedColumnSide { case .left: - let x = Array(0.. UICollectionViewLayoutAttributes? { - guard let attributes = attributes else { return nil } - if columnPosition == 0 || columnPosition == 1 { - let x = Array(0.. UICollectionViewLayoutAttributes? { +// guard let attributes = attributes else { return nil } +// if columnPosition == 0 || columnPosition == 1 { +// let x = Array(0.. UICollectionViewLayoutAttributes? { let kind = SwiftDataTable.SupplementaryViewType(kind: elementKind) switch kind { case .searchHeader: return self.layoutAttributesForHeaderView(at: indexPath) case .columnHeader: - var attributes = layoutAttributesForColumnHeaderView(at: indexPath) - return self.adjustSupplementaryView(attributes: attributes, at: indexPath[0]) + let attributes = layoutAttributesForColumnHeaderView(at: indexPath) + return adjustAttributesPosition(attributes, at: indexPath[0], zIndexPosition: 100+indexPath[0]) case .footerHeader: - return self.layoutAttributesForColumnFooterView(at: indexPath) + let attributes = layoutAttributesForColumnFooterView(at: indexPath) + return adjustAttributesPosition(attributes, at: indexPath[0], zIndexPosition: 100+indexPath[0]) case .paginationHeader: return self.layoutAttributesForPaginationView(at: indexPath) } } @@ -325,7 +327,7 @@ extension SwiftDataTableLayout { if self.dataTable.shouldSectionFootersFloat(){ let yOffsetBottomView: CGFloat = self.collectionView!.contentOffset.y + self.collectionView!.bounds.height - height - self.dataTable.heightForPaginationView() // - height attribute.frame.origin.y = yOffsetBottomView - attribute.zIndex = 100 + attribute.zIndex += 1 } return attribute }