Skip to content

Commit

Permalink
fix: crash occuring when the datasource is empty. updated project to …
Browse files Browse the repository at this point in the history
…show empty state
  • Loading branch information
pavankataria committed Mar 7, 2019
1 parent ca764fb commit 509a5bd
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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))")
}
}
50 changes: 31 additions & 19 deletions Example/DemoSwiftDataTables/MenuTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)]")
Expand All @@ -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]
Expand Down Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion SwiftDataTables/Classes/DataStructureModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 8 additions & 1 deletion SwiftDataTables/Classes/SwiftDataTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 509a5bd

Please sign in to comment.