From aecad0944566fd0f609dd6283199f47ad3739c11 Mon Sep 17 00:00:00 2001 From: Pavan Kataria Date: Thu, 6 Apr 2017 00:09:49 +0100 Subject: [PATCH] Implements Data Source --- .../SwiftDataTables.xcodeproj/project.pbxproj | 11 ++ .../Base.lproj/Main.storyboard | 105 ++++++++++++++++-- ...ataTableWithDataSourceViewController.swift | 78 +++++++++++++ .../SwiftDataTables_Example-Bridging-Header.h | 4 + Example/SwiftDataTables/ViewController.swift | 5 +- SwiftDataTables/Classes/SwiftDataTable.swift | 84 ++++++++++++-- 6 files changed, 266 insertions(+), 21 deletions(-) create mode 100644 Example/SwiftDataTables/DataTableWithDataSourceViewController.swift create mode 100644 Example/SwiftDataTables/SwiftDataTables_Example-Bridging-Header.h diff --git a/Example/SwiftDataTables.xcodeproj/project.pbxproj b/Example/SwiftDataTables.xcodeproj/project.pbxproj index 0b59307..d3a89dd 100644 --- a/Example/SwiftDataTables.xcodeproj/project.pbxproj +++ b/Example/SwiftDataTables.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 058D4BB71E954000002A2DC9 /* DataTableWithDataSourceViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 058D4BB61E954000002A2DC9 /* DataTableWithDataSourceViewController.swift */; }; 05DDE6B11E71AF2C003BD61E /* File.swift in Sources */ = {isa = PBXBuildFile; fileRef = 05DDE6B01E71AF2C003BD61E /* File.swift */; }; 607FACD61AFB9204008FA782 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACD51AFB9204008FA782 /* AppDelegate.swift */; }; 607FACD81AFB9204008FA782 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACD71AFB9204008FA782 /* ViewController.swift */; }; @@ -29,6 +30,8 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 058D4BB21E953FDD002A2DC9 /* SwiftDataTables_Example-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SwiftDataTables_Example-Bridging-Header.h"; sourceTree = ""; }; + 058D4BB61E954000002A2DC9 /* DataTableWithDataSourceViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DataTableWithDataSourceViewController.swift; sourceTree = ""; }; 05DDE6B01E71AF2C003BD61E /* File.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = File.swift; sourceTree = ""; }; 5076460E54A93B08419E4FC3 /* Pods_SwiftDataTables_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SwiftDataTables_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 50B52C9654BFE2DA84A4C929 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = ""; }; @@ -109,10 +112,12 @@ 607FACD51AFB9204008FA782 /* AppDelegate.swift */, 05DDE6B01E71AF2C003BD61E /* File.swift */, 607FACD71AFB9204008FA782 /* ViewController.swift */, + 058D4BB61E954000002A2DC9 /* DataTableWithDataSourceViewController.swift */, 607FACD91AFB9204008FA782 /* Main.storyboard */, 607FACDC1AFB9204008FA782 /* Images.xcassets */, 607FACDE1AFB9204008FA782 /* LaunchScreen.xib */, 607FACD31AFB9204008FA782 /* Supporting Files */, + 058D4BB21E953FDD002A2DC9 /* SwiftDataTables_Example-Bridging-Header.h */, ); name = "Example for SwiftDataTables"; path = SwiftDataTables; @@ -368,6 +373,7 @@ files = ( 607FACD81AFB9204008FA782 /* ViewController.swift in Sources */, 05DDE6B11E71AF2C003BD61E /* File.swift in Sources */, + 058D4BB71E954000002A2DC9 /* DataTableWithDataSourceViewController.swift in Sources */, 607FACD61AFB9204008FA782 /* AppDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -503,6 +509,7 @@ buildSettings = { ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CLANG_ENABLE_MODULES = YES; DEVELOPMENT_TEAM = R85C7FUGTC; INFOPLIST_FILE = SwiftDataTables/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 8.0; @@ -510,6 +517,8 @@ MODULE_NAME = ExampleApp; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OBJC_BRIDGING_HEADER = "SwiftDataTables/SwiftDataTables_Example-Bridging-Header.h"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; }; @@ -521,6 +530,7 @@ buildSettings = { ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CLANG_ENABLE_MODULES = YES; DEVELOPMENT_TEAM = R85C7FUGTC; INFOPLIST_FILE = SwiftDataTables/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 8.0; @@ -528,6 +538,7 @@ MODULE_NAME = ExampleApp; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OBJC_BRIDGING_HEADER = "SwiftDataTables/SwiftDataTables_Example-Bridging-Header.h"; SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; }; diff --git a/Example/SwiftDataTables/Base.lproj/Main.storyboard b/Example/SwiftDataTables/Base.lproj/Main.storyboard index ae67c21..0bbe79d 100644 --- a/Example/SwiftDataTables/Base.lproj/Main.storyboard +++ b/Example/SwiftDataTables/Base.lproj/Main.storyboard @@ -4,28 +4,111 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + - + - - + + - + - - + + - - + - + @@ -38,7 +121,7 @@ - + diff --git a/Example/SwiftDataTables/DataTableWithDataSourceViewController.swift b/Example/SwiftDataTables/DataTableWithDataSourceViewController.swift new file mode 100644 index 0000000..8d86b17 --- /dev/null +++ b/Example/SwiftDataTables/DataTableWithDataSourceViewController.swift @@ -0,0 +1,78 @@ +// +// DataTableWithDataSourceViewController.swift +// SwiftDataTables +// +// Created by Pavan Kataria on 05/04/2017. +// Copyright © 2017 CocoaPods. All rights reserved. +// + +import UIKit +import SwiftDataTables + +class DataTableWithDataSourceViewController: UIViewController { + + var dataTable: SwiftDataTable! = nil + var dataSource: DataTableContent = [] + + let headerTitles = ["Name", "Fav Beverage", "Fav language", "Short term goals", "Height"] + + override func viewDidLoad() { + super.viewDidLoad() + self.automaticallyAdjustsScrollViewInsets = false + self.navigationController?.navigationBar.isTranslucent = false + self.title = "Streaming fans" + self.view.backgroundColor = UIColor.white + + self.dataTable = SwiftDataTable(dataSource: self) + self.dataTable.autoresizingMask = [.flexibleWidth, .flexibleHeight] + self.dataTable.frame = self.view.frame + self.view.addSubview(self.dataTable); + + self.addDataSourceAfter() + } + public func addDataSourceAfter(){ + + self.dataSource = [[ + DataTableValueType.string("Pavan"), + DataTableValueType.string("Juice"), + DataTableValueType.string("Swift and Php"), + DataTableValueType.string("Be a game publisher"), + DataTableValueType.float(175.25) + ], + [ + DataTableValueType.string("NoelDavies"), + DataTableValueType.string("Water"), + DataTableValueType.string("Php and Javascript"), + DataTableValueType.string("'Be a fucking paratrooper machine'"), + DataTableValueType.float(185.80) + ], + [ + DataTableValueType.string("Redsaint"), + DataTableValueType.string("Cheerwine and Dr.Pepper"), + DataTableValueType.string("Java"), + DataTableValueType.string("'Creating an awesome RPG Game game'"), + DataTableValueType.float(185.42) + ], + ] + + self.dataTable.reload() + } +} + +extension DataTableWithDataSourceViewController: SwiftDataTableDataSource { + public func dataTable(_ dataTable: SwiftDataTable, headerTitleForColumnAt columnIndex: NSInteger) -> String { + return self.headerTitles[columnIndex] + } + + public func numberOfColumns(in: SwiftDataTable) -> Int { + return 4 + } + + func numberOfRows(in: SwiftDataTable) -> Int { + return self.dataSource.count + } + + public func dataTable(_ dataTable: SwiftDataTable, dataForRowAt index: NSInteger) -> [DataTableValueType] { + return self.dataSource[index] + } +} diff --git a/Example/SwiftDataTables/SwiftDataTables_Example-Bridging-Header.h b/Example/SwiftDataTables/SwiftDataTables_Example-Bridging-Header.h new file mode 100644 index 0000000..1b2cb5d --- /dev/null +++ b/Example/SwiftDataTables/SwiftDataTables_Example-Bridging-Header.h @@ -0,0 +1,4 @@ +// +// Use this file to import your target's public headers that you would like to expose to Swift. +// + diff --git a/Example/SwiftDataTables/ViewController.swift b/Example/SwiftDataTables/ViewController.swift index e4adac3..f0dfea4 100644 --- a/Example/SwiftDataTables/ViewController.swift +++ b/Example/SwiftDataTables/ViewController.swift @@ -32,15 +32,14 @@ class ViewController: UIViewController { var options = DataTableConfiguration() options.defaultOrdering = DataTableColumnOrder(index: 1, order: .ascending) - - + self.dataTable = SwiftDataTable( data: self.data(), headerTitles: self.columnHeaders(), options: options ) self.automaticallyAdjustsScrollViewInsets = false - + self.dataTable.backgroundColor = UIColor.init(red: 235/255, green: 235/255, blue: 235/255, alpha: 1) //25/255, green: 33/255, blue: 39/255, alpha: 1) diff --git a/SwiftDataTables/Classes/SwiftDataTable.swift b/SwiftDataTables/Classes/SwiftDataTable.swift index b4e0e2b..8d351d1 100644 --- a/SwiftDataTables/Classes/SwiftDataTable.swift +++ b/SwiftDataTables/Classes/SwiftDataTable.swift @@ -8,8 +8,18 @@ import UIKit -public typealias DataTableContent = [[DataTableValueType]] +public typealias DataTableRow = [DataTableValueType] +public typealias DataTableContent = [DataTableRow] public typealias DataTableViewModelContent = [[DataCellViewModel]] + + +public protocol SwiftDataTableDataSource { + func numberOfColumns(in: SwiftDataTable) -> Int + func numberOfRows(in: SwiftDataTable) -> Int + func dataTable(_ dataTable: SwiftDataTable, dataForRowAt index: NSInteger) -> [DataTableValueType] + func dataTable(_ dataTable: SwiftDataTable, headerTitleForColumnAt columnIndex: NSInteger) -> String +} + public class SwiftDataTable: UIView { public enum SupplementaryViewType: String { /// Single header positioned at the top above the column section @@ -32,6 +42,9 @@ public class SwiftDataTable: UIView { } } + var dataSource: SwiftDataTableDataSource! + var options: DataTableConfiguration + let highlightedColours = [ UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1), UIColor(red: 0.9725, green: 0.9725, blue: 0.9725, alpha: 1) @@ -161,11 +174,23 @@ public class SwiftDataTable: UIView { // }() //MARK: - Lifecycle + public init(dataSource: SwiftDataTableDataSource, + options: DataTableConfiguration? = DataTableConfiguration(), + frame: CGRect = .zero){ + self.options = options! + super.init(frame: frame) + self.dataSource = dataSource + + self.set(options: options) + self.registerObservers() + } + public init(data: DataTableContent, headerTitles: [String], - options: DataTableConfiguration = DataTableConfiguration(), + options: DataTableConfiguration? = DataTableConfiguration(), frame: CGRect = .zero) { + self.options = options! super.init(frame: frame) self.set(data: data, headerTitles: headerTitles, options: options) self.registerObservers() @@ -218,7 +243,7 @@ public class SwiftDataTable: UIView { collectionView.register(UINib(nibName: menuLengthIdentifier, bundle: podBundle), forSupplementaryViewOfKind: SupplementaryViewType.searchHeader.rawValue, withReuseIdentifier: menuLengthIdentifier) } - func set(data: [[DataTableValueType]], headerTitles: [String], options: DataTableConfiguration? = nil){ + func set(data: DataTableContent, headerTitles: [String], options: DataTableConfiguration? = nil){ self.dataStructure = DataStructureModel(data: data, headerTitles: headerTitles) self.createDataCellViewModels(with: self.dataStructure) self.layout = SwiftDataTableFlowLayout(dataTable: self) @@ -240,12 +265,13 @@ public class SwiftDataTable: UIView { for columnIndex in Array(0.. Int { + if let dataSource = self.dataSource { + return dataSource.numberOfColumns(in: self) + } return self.dataStructure.columnCount } public func numberOfSections(in collectionView: UICollectionView) -> Int { + //if let dataSource = self.dataSource { + // return dataSource.numberOfRows(in: self) + //} return self.numberOfRows() } public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { - let cell = self.rowModel(at: indexPath).dequeueCell(collectionView: collectionView, indexPath: indexPath) + let cellViewModel: DataCellViewModel + //if let dataSource = self.dataSource { + // cellViewModel = dataSource.dataTable(self, dataForRowAt: indexPath.row) + //} + //else { + cellViewModel = self.rowModel(at: indexPath) + //} + let cell = cellViewModel.dequeueCell(collectionView: collectionView, indexPath: indexPath) return cell } public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { @@ -452,7 +509,8 @@ extension SwiftDataTable { } extension SwiftDataTable { - func update(){ + + fileprivate func update(){ // print("\nUpdate") self.reloadEverything() } @@ -600,6 +658,7 @@ extension SwiftDataTable { return Array(0.. CGFloat { return 44 } @@ -750,3 +809,14 @@ extension SwiftDataTable: UISearchBarDelegate { }) } } + + +extension SwiftDataTable { + func set(options: DataTableConfiguration? = nil){ + self.layout = SwiftDataTableFlowLayout(dataTable: self) + self.rowViewModels = DataTableViewModelContent() + self.paginationViewModel = PaginationHeaderViewModel() + self.menuLengthViewModel = MenuLengthHeaderViewModel() + //self.reload(); + } +}