From e377393a0a67ebd78a5b77c9d9abdd18cd2461ed Mon Sep 17 00:00:00 2001 From: Cwep Date: Wed, 16 Oct 2019 11:42:20 +0200 Subject: [PATCH 1/2] Added customization options for configuration to allow more styling --- .../Classes/DataCell/DataCell.swift | 6 ++++++ .../Classes/DataCell/DataCellViewModel.swift | 16 +++++++++++++-- .../HeaderFooter/DataHeaderFooter.swift | 8 ++++++++ .../Classes/HeaderFooter/DataHeaderFooter.xib | 20 ++++++++++++------- .../DataHeaderFooterViewModel.swift | 12 +++++++++++ .../Models/DataTableConfiguration.swift | 7 +++++++ SwiftDataTables/Classes/SwiftDataTable.swift | 2 +- 7 files changed, 61 insertions(+), 10 deletions(-) diff --git a/SwiftDataTables/Classes/DataCell/DataCell.swift b/SwiftDataTables/Classes/DataCell/DataCell.swift index 5df8e93..c77a36a 100755 --- a/SwiftDataTables/Classes/DataCell/DataCell.swift +++ b/SwiftDataTables/Classes/DataCell/DataCell.swift @@ -17,5 +17,11 @@ class DataCell: UICollectionViewCell { func setup(_ viewModel: DataCellViewModel){ self.dataLabel.text = viewModel.data.stringRepresentation // self.contentView.backgroundColor = .white + self.dataLabel.textAlignment = viewModel.dataTextAlignement ?? .natural + guard let showBorders = viewModel.shouldShowDataBorders else { return } + if showBorders { + self.layer.borderWidth = 1.0 + self.layer.borderColor = UIColor.black.cgColor + } } } diff --git a/SwiftDataTables/Classes/DataCell/DataCellViewModel.swift b/SwiftDataTables/Classes/DataCell/DataCellViewModel.swift index c21a529..e4c3701 100755 --- a/SwiftDataTables/Classes/DataCell/DataCellViewModel.swift +++ b/SwiftDataTables/Classes/DataCell/DataCellViewModel.swift @@ -10,7 +10,9 @@ import Foundation import UIKit open class DataCellViewModel: VirtualPositionTrackable, CollectionViewCellRepresentable { - + //MARK: - Private Properties + private var dataTableConfiguration: DataTableConfiguration? + //MARK: - Public Properties var xPositionRunningTotal: CGFloat? = nil var yPositionRunningTotal: CGFloat? = nil @@ -21,9 +23,19 @@ open class DataCellViewModel: VirtualPositionTrackable, CollectionViewCellRepres public var stringRepresentation: String { return self.data.stringRepresentation } + + public var shouldShowDataBorders: Bool? { + return dataTableConfiguration?.shouldShowDataBorders ?? false + } + + public var dataTextAlignement: NSTextAlignment? { + return dataTableConfiguration?.dataTextAlignment ?? .natural + } + //MARK: - Lifecycle - init(data: DataTableValueType){ + init(data: DataTableValueType, dataTableConfiguration: DataTableConfiguration?){ self.data = data + self.dataTableConfiguration = dataTableConfiguration } static func registerCell(collectionView: UICollectionView) { diff --git a/SwiftDataTables/Classes/HeaderFooter/DataHeaderFooter.swift b/SwiftDataTables/Classes/HeaderFooter/DataHeaderFooter.swift index 4634d1a..11d4890 100755 --- a/SwiftDataTables/Classes/HeaderFooter/DataHeaderFooter.swift +++ b/SwiftDataTables/Classes/HeaderFooter/DataHeaderFooter.swift @@ -11,6 +11,7 @@ import UIKit class DataHeaderFooter: UICollectionReusableView { //MARK: - Properties + @IBOutlet var backgroundView: UIView! @IBOutlet var titleLabel: UILabel! @IBOutlet var sortingImageView: UIImageView! @@ -26,9 +27,16 @@ class DataHeaderFooter: UICollectionReusableView { func setup(viewModel: DataHeaderFooterViewModel) { self.titleLabel.text = viewModel.data + self.titleLabel.textAlignment = viewModel.headerFooterTextAlignment ?? .natural self.sortingImageView.image = viewModel.imageForSortingElement self.sortingImageView.tintColor = viewModel.tintColorForSortingElement self.backgroundColor = .white + self.backgroundView.backgroundColor = viewModel.backgroundColorForHeaderFooter ?? UIColor.clear + guard let showBorders = viewModel.shouldShowHeaderFooterBorders else { return } + if showBorders { + self.layer.borderWidth = 1.0 + self.layer.borderColor = UIColor.black.cgColor + } } @objc func didTapView(){ self.didTapEvent?() diff --git a/SwiftDataTables/Classes/HeaderFooter/DataHeaderFooter.xib b/SwiftDataTables/Classes/HeaderFooter/DataHeaderFooter.xib index 64ca0df..458efaf 100755 --- a/SwiftDataTables/Classes/HeaderFooter/DataHeaderFooter.xib +++ b/SwiftDataTables/Classes/HeaderFooter/DataHeaderFooter.xib @@ -1,12 +1,9 @@ - - - - + + - - + @@ -16,6 +13,10 @@ + + + + - + @@ -34,14 +35,19 @@ + + + + + diff --git a/SwiftDataTables/Classes/HeaderFooter/DataHeaderFooterViewModel.swift b/SwiftDataTables/Classes/HeaderFooter/DataHeaderFooterViewModel.swift index 882553c..6b53ee4 100755 --- a/SwiftDataTables/Classes/HeaderFooter/DataHeaderFooterViewModel.swift +++ b/SwiftDataTables/Classes/HeaderFooter/DataHeaderFooterViewModel.swift @@ -49,6 +49,18 @@ public class DataHeaderFooterViewModel: DataTableSortable { var tintColorForSortingElement: UIColor? { return (dataTable != nil && sortType != .unspecified) ? dataTable.options.sortArrowTintColor : UIColor.gray } + + var backgroundColorForHeaderFooter: UIColor? { + return (dataTable != nil) ? dataTable.options.headerFooterBackgroundColor : UIColor.white + } + + var shouldShowHeaderFooterBorders: Bool? { + return (dataTable != nil) ? dataTable.options.shouldShowHeaderFooterBorders : false + } + + var headerFooterTextAlignment: NSTextAlignment? { + return (dataTable != nil) ? dataTable.options.headerFooterTextAlignment : .natural + } //MARK: - Events diff --git a/SwiftDataTables/Classes/Models/DataTableConfiguration.swift b/SwiftDataTables/Classes/Models/DataTableConfiguration.swift index 61bf3b7..43add84 100755 --- a/SwiftDataTables/Classes/Models/DataTableConfiguration.swift +++ b/SwiftDataTables/Classes/Models/DataTableConfiguration.swift @@ -25,6 +25,8 @@ public struct DataTableConfiguration: Equatable { public var heightForSearchView: CGFloat = 60 public var heightOfInterRowSpacing: CGFloat = 1 + public var shouldShowDataBorders: Bool = false + public var shouldShowHeaderFooterBorders: Bool = false public var shouldShowFooter: Bool = true public var shouldShowSearchSection: Bool = true public var shouldSearchHeaderFloat: Bool = false @@ -49,6 +51,11 @@ public struct DataTableConfiguration: Equatable { ] public var fixedColumns: DataTableFixedColumnType? = nil + + public var headerFooterBackgroundColor: UIColor = UIColor.white + + public var dataTextAlignment: NSTextAlignment = .natural + public var headerFooterTextAlignment: NSTextAlignment = .natural public init(){ diff --git a/SwiftDataTables/Classes/SwiftDataTable.swift b/SwiftDataTables/Classes/SwiftDataTable.swift index 47430cd..20423d5 100755 --- a/SwiftDataTables/Classes/SwiftDataTable.swift +++ b/SwiftDataTables/Classes/SwiftDataTable.swift @@ -341,7 +341,7 @@ public extension SwiftDataTable { //let viewModels: DataTableViewModelContent = self.rowViewModels = dataStructure.data.map{ currentRowData in return currentRowData.map { - return DataCellViewModel(data: $0) + return DataCellViewModel(data: $0, dataTableConfiguration: options) } } self.paginationViewModel = PaginationHeaderViewModel() From c783b86420b19692d4944eca782e5ed1f32dd8e5 Mon Sep 17 00:00:00 2001 From: Cwep Date: Fri, 25 Oct 2019 19:12:14 +0200 Subject: [PATCH 2/2] Added UI examples for customization --- .../MenuTableViewController.swift | 24 +++++++++++++++++++ SwiftDataTables/Classes/SwiftDataTable.swift | 1 + 2 files changed, 25 insertions(+) diff --git a/Example/DemoSwiftDataTables/MenuTableViewController.swift b/Example/DemoSwiftDataTables/MenuTableViewController.swift index 15af504..abf966d 100644 --- a/Example/DemoSwiftDataTables/MenuTableViewController.swift +++ b/Example/DemoSwiftDataTables/MenuTableViewController.swift @@ -173,6 +173,9 @@ extension MenuViewController { 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())) + section.append(MenuItem(title: "With borders", config: configurationBorders())) + section.append(MenuItem(title: "Header with background colour", config: configurationColouredHeader())) + section.append(MenuItem(title: "With centered text", config: configurationCenteredText())) return section } } @@ -225,6 +228,27 @@ extension MenuViewController { ] return configuration } + + private func configurationBorders() -> DataTableConfiguration { + var configuration = DataTableConfiguration() + configuration.shouldShowDataBorders = true + configuration.shouldShowHeaderFooterBorders = true + return configuration + } + + private func configurationCenteredText() -> DataTableConfiguration { + var configuration = DataTableConfiguration() + configuration.dataTextAlignment = .center + configuration.headerFooterTextAlignment = .center + return configuration + } + + private func configurationColouredHeader() -> DataTableConfiguration { + var configuration = DataTableConfiguration() + configuration.headerFooterBackgroundColor = UIColor(1, 0.90, 0.90) + return configuration + } + private func configurationFixedColumns() -> DataTableConfiguration { var configuration = DataTableConfiguration() configuration.fixedColumns = DataTableFixedColumnType(leftColumns: 1, rightColumns: 1) diff --git a/SwiftDataTables/Classes/SwiftDataTable.swift b/SwiftDataTables/Classes/SwiftDataTable.swift index 20423d5..a27d688 100755 --- a/SwiftDataTables/Classes/SwiftDataTable.swift +++ b/SwiftDataTables/Classes/SwiftDataTable.swift @@ -334,6 +334,7 @@ public extension SwiftDataTable { data: dataStructure.footerTitles[$0], sortType: sortTypeForFooter ) + headerViewModel.configure(dataTable: self, columnIndex: $0) return headerViewModel }