From 509a5bd6913e026ce580a1f22119b24a8df66e02 Mon Sep 17 00:00:00 2001 From: Pavan Kataria Date: Thu, 7 Mar 2019 22:46:43 +0000 Subject: [PATCH] fix: crash occuring when the datasource is empty. updated project to show empty state --- .../DataTableWithDataSetViewController.swift | 2 +- ...ataTableWithDataSourceViewController.swift | 90 ++++++++++--------- .../MenuTableViewController.swift | 50 +++++++---- .../Classes/DataStructureModel.swift | 2 +- SwiftDataTables/Classes/SwiftDataTable.swift | 9 +- 5 files changed, 90 insertions(+), 63 deletions(-) diff --git a/Example/DemoSwiftDataTables/DataTableWithDataSetViewController.swift b/Example/DemoSwiftDataTables/DataTableWithDataSetViewController.swift index ed4783a..3138303 100755 --- a/Example/DemoSwiftDataTables/DataTableWithDataSetViewController.swift +++ b/Example/DemoSwiftDataTables/DataTableWithDataSetViewController.swift @@ -41,7 +41,7 @@ class DataTableWithDataSetViewController: UIViewController { } extension DataTableWithDataSetViewController: SwiftDataTableDelegate { func didSelectItem(_ dataTable: SwiftDataTable, indexPath: IndexPath) { - print("did select item at indexPath: \(indexPath)") + print("did select item at indexPath: \(indexPath) dataValue: \(dataTable.data(for: indexPath))") } } extension DataTableWithDataSetViewController { diff --git a/Example/DemoSwiftDataTables/DataTableWithDataSourceViewController.swift b/Example/DemoSwiftDataTables/DataTableWithDataSourceViewController.swift index 8047a2a..a5517b2 100755 --- a/Example/DemoSwiftDataTables/DataTableWithDataSourceViewController.swift +++ b/Example/DemoSwiftDataTables/DataTableWithDataSourceViewController.swift @@ -9,56 +9,64 @@ import UIKit class DataTableWithDataSourceViewController: UIViewController { - - var dataTable: SwiftDataTable! = nil - var dataSource: DataTableContent = [] + lazy var dataTable = makeDataTable() + var dataSource: DataTableContent = [] let headerTitles = ["Name", "Fav Beverage", "Fav language", "Short term goals", "Height"] - + + init() { + super.init(nibName: nil, bundle: nil) + } + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } 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.delegate = self - self.dataTable.autoresizingMask = [.flexibleWidth, .flexibleHeight] - self.dataTable.frame = self.view.frame - self.view.addSubview(self.dataTable); - - self.addDataSourceAfter() + automaticallyAdjustsScrollViewInsets = false + navigationController?.navigationBar.isTranslucent = false + title = "Streaming fans" + view.backgroundColor = UIColor.white + dataTable.frame = view.frame + view.addSubview(dataTable) + dataTable.reload() } + 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.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() + dataTable.reload() + } +} +extension DataTableWithDataSourceViewController { + private func makeDataTable() -> SwiftDataTable { + let dataTable = SwiftDataTable(dataSource: self) + dataTable.delegate = self + dataTable.autoresizingMask = [.flexibleWidth, .flexibleHeight] + return dataTable } } - extension DataTableWithDataSourceViewController: SwiftDataTableDataSource { public func dataTable(_ dataTable: SwiftDataTable, headerTitleForColumnAt columnIndex: NSInteger) -> String { return self.headerTitles[columnIndex] @@ -79,6 +87,6 @@ extension DataTableWithDataSourceViewController: SwiftDataTableDataSource { extension DataTableWithDataSourceViewController: SwiftDataTableDelegate { func didSelectItem(_ dataTable: SwiftDataTable, indexPath: IndexPath) { - print("did select item at indexPath: \(indexPath)") + print("did select item at indexPath: \(indexPath) dataValue: \(dataTable.data(for: indexPath))") } } diff --git a/Example/DemoSwiftDataTables/MenuTableViewController.swift b/Example/DemoSwiftDataTables/MenuTableViewController.swift index f1e7848..7cda5e9 100644 --- a/Example/DemoSwiftDataTables/MenuTableViewController.swift +++ b/Example/DemoSwiftDataTables/MenuTableViewController.swift @@ -26,7 +26,6 @@ class MenuViewController: UITableViewController { } static let menuItemIdentifier: String = "MenuItemIdentifier" } -// lazy var exampleConfigurations: [menuItems] = self.createdExampleConfigurations() lazy var menuItems: [[MenuItem]] = self.createdMenuItems() lazy var exampleConfigurations: [MenuItem] = self.createdExampleConfigurations() @@ -40,11 +39,16 @@ class MenuViewController: UITableViewController { //MARK: - Actions private func showDataStoreWithDataSet(){ let instance = DataTableWithDataSetViewController() - self.show(instance, sender: self) + show(instance, sender: self) } private func showDataStoreWithDataSource(){ let instance = DataTableWithDataSourceViewController() - self.show(instance, sender: self) + show(instance, sender: self) + instance.addDataSourceAfter() + } + private func showDataStoreWithEmptyDataSet() { + let instance = DataTableWithDataSourceViewController() + show(instance, sender: self) } private func showGenericExample(for index: Int){ let menuItem = self.exampleConfigurations[index] @@ -109,7 +113,7 @@ class GenericDataTableViewController: UIViewController { extension GenericDataTableViewController: SwiftDataTableDelegate { func didSelectItem(_ dataTable: SwiftDataTable, indexPath: IndexPath) { - print("did select item at indexPath: \(indexPath)") + print("did select item at indexPath: \(indexPath) dataValue: \(dataTable.data(for: indexPath))") } } //MARK: - Data source and delegate methods @@ -141,6 +145,8 @@ extension MenuViewController { showDataStoreWithDataSet() case (0,1): showDataStoreWithDataSource() + case (0, 2): + showDataStoreWithEmptyDataSet() case (1, let row): showGenericExample(for: row) default: fatalError("An example hasn't been created for [section: \(indexPath.section) row: \(indexPath.row)]") @@ -152,7 +158,8 @@ extension MenuViewController { private func createdMenuItems() -> [[MenuItem]] { let sectionOne: [MenuItem] = [ MenuItem(title: "Initialised with Data Set"), - MenuItem(title: "Initialised with Data Source")] + MenuItem(title: "Initialised with Data Source"), + MenuItem(title: "Initialised with Empty Data Source")] let sectionTwo = self.exampleConfigurations return [sectionOne, sectionTwo] @@ -197,24 +204,29 @@ extension MenuViewController { private func configurationAlternatingColours() -> DataTableConfiguration { var configuration = DataTableConfiguration() configuration.highlightedAlternatingRowColors = [ - UIColor(red: 1, green: 0.7, blue: 0.7, alpha: 1), - UIColor(red: 1, green: 0.7, blue: 0.5, alpha: 1), - UIColor(red: 1, green: 1, blue: 0.5, alpha: 1), - UIColor(red: 0.5, green: 1, blue: 0.5, alpha: 1), - UIColor(red: 0.5, green: 0.7, blue: 1, alpha: 1), - UIColor(red: 0.5, green: 0.5, blue: 1, alpha: 1), - UIColor(red: 1, green: 0.5, blue: 0.5, alpha: 1) + .init(1, 0.7, 0.7), + .init(1, 0.7, 0.5), + .init(1, 1, 0.5), + .init(0.5, 1, 0.5), + .init(0.5, 0.7, 1), + .init(0.5, 0.5, 1), + .init(1, 0.5, 0.5) ] configuration.unhighlightedAlternatingRowColors = [ - UIColor(red: 1, green: 0.90, blue: 0.90, alpha: 1), - UIColor(red: 1, green: 0.90, blue: 0.7, alpha: 1), - UIColor(red: 1, green: 1, blue: 0.7, alpha: 1), - UIColor(red: 0.7, green: 1, blue: 0.7, alpha: 1), - UIColor(red: 0.7, green: 0.9, blue: 1, alpha: 1), - UIColor(red: 0.7, green: 0.7, blue: 1, alpha: 1), - UIColor(red: 1, green: 0.7, blue: 0.7, alpha: 1) + .init(1, 0.90, 0.90), + .init(1, 0.90, 0.7), + .init(1, 1, 0.7), + .init(0.7, 1, 0.7), + .init(0.7, 0.9, 1), + .init(0.7, 0.7, 1), + .init(1, 0.7, 0.7) ] return configuration } } +extension UIColor { + public convenience init(_ r: CGFloat, _ g: CGFloat, _ b: CGFloat, _ a: CGFloat = 1){ + self.init(red: r, green: g, blue: b, alpha: a) + } +} diff --git a/SwiftDataTables/Classes/DataStructureModel.swift b/SwiftDataTables/Classes/DataStructureModel.swift index 447c772..ca07022 100755 --- a/SwiftDataTables/Classes/DataStructureModel.swift +++ b/SwiftDataTables/Classes/DataStructureModel.swift @@ -79,7 +79,7 @@ public struct DataStructureModel { let dataType: DataTableValueType = data[$1][column] return $0 + dataType.stringRepresentation.count } - columnContentAverages.append(Float(averageForCurrentColumn)/Float(data.count)) + columnContentAverages.append((data.count == 0) ? 1 : Float(averageForCurrentColumn) / Float(data.count)) } return columnContentAverages } diff --git a/SwiftDataTables/Classes/SwiftDataTable.swift b/SwiftDataTables/Classes/SwiftDataTable.swift index fa5d311..fa866bd 100755 --- a/SwiftDataTables/Classes/SwiftDataTable.swift +++ b/SwiftDataTables/Classes/SwiftDataTable.swift @@ -37,9 +37,12 @@ public class SwiftDataTable: UIView { public weak var dataSource: SwiftDataTableDataSource? public weak var delegate: SwiftDataTableDelegate? + public var rows: DataTableViewModelContent { + return self.currentRowViewModels + } + var options: DataTableConfiguration - //MARK: - Private Properties var currentRowViewModels: DataTableViewModelContent { get { @@ -301,6 +304,10 @@ public class SwiftDataTable: UIView { self.set(data: data, headerTitles: headerTitles, options: self.options) self.collectionView.reloadData() } + + public func data(for indexPath: IndexPath) -> DataTableValueType { + return rows[indexPath.section][indexPath.row].data + } } public extension SwiftDataTable {