parent
d2b6f539f1
commit
f64ac3ed27
@ -1,63 +0,0 @@ |
||||
// |
||||
// Array+UITableDataSource.swift |
||||
// TournamentStats |
||||
// |
||||
// Created by Laurent Morvillier on 05/08/2019. |
||||
// Copyright © 2019 Stax River. All rights reserved. |
||||
// |
||||
|
||||
import Foundation |
||||
import UIKit |
||||
|
||||
|
||||
|
||||
|
||||
class DataSourceWrapper<T : ColumnRepresentable> : NSObject, UITableViewDataSource, UITableViewDelegate { |
||||
|
||||
var columnRepresentables: [T] |
||||
|
||||
init(array: [T]) { |
||||
self.columnRepresentables = array |
||||
} |
||||
|
||||
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
||||
print("numberOfRowsInSection = \(self.columnRepresentables.count)") |
||||
return self.columnRepresentables.count + 1 // + 1 for header |
||||
} |
||||
|
||||
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { |
||||
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! StackTableCell |
||||
|
||||
var columns: [String] |
||||
var font: UIFont |
||||
switch indexPath.row { |
||||
case 0: |
||||
columns = T.headers() |
||||
font = UIFont.boldSystemFont(ofSize: 16.0) |
||||
default: |
||||
let cr = self.columnRepresentables[indexPath.row - 1] |
||||
columns = cr.colums() |
||||
font = UIFont.systemFont(ofSize: 16.0) |
||||
} |
||||
|
||||
columns.forEach { value in |
||||
let label: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 30)) |
||||
label.textColor = UIColor.white |
||||
label.backgroundColor = UIColor.clear |
||||
label.text = value |
||||
label.font = font |
||||
cell.stackView.addArrangedSubview(label) |
||||
} |
||||
|
||||
return cell |
||||
} |
||||
|
||||
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { |
||||
return 32.0 |
||||
} |
||||
|
||||
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { |
||||
return 0.1 |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,86 @@ |
||||
// |
||||
// Array+UITableDataSource.swift |
||||
// TournamentStats |
||||
// |
||||
// Created by Laurent Morvillier on 05/08/2019. |
||||
// Copyright © 2019 Stax River. All rights reserved. |
||||
// |
||||
|
||||
import Foundation |
||||
import UIKit |
||||
|
||||
|
||||
|
||||
|
||||
class DataSourceWrapper<T : ColumnRepresentable> : NSObject, UITableViewDataSource, UITableViewDelegate { |
||||
|
||||
var columnRepresentables: [T] |
||||
let columnDescriptors = T.columnDescriptors() |
||||
var totalWidthWeigth: CGFloat = 0.0 |
||||
|
||||
init(array: [T]) { |
||||
self.columnRepresentables = array |
||||
self.totalWidthWeigth = self.columnDescriptors.map { $0.widthWeight }.reduce(0, +) |
||||
} |
||||
|
||||
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
||||
print("numberOfRowsInSection = \(self.columnRepresentables.count)") |
||||
return self.columnRepresentables.count + 1 // + 1 for header |
||||
} |
||||
|
||||
fileprivate let FONTSIZE: CGFloat = 18.0 |
||||
|
||||
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { |
||||
// let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! StackTableCell |
||||
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")! |
||||
|
||||
var cells: [String] |
||||
switch indexPath.row { |
||||
case 0: |
||||
cells = columnDescriptors.map { $0.header } |
||||
default: |
||||
let cr = self.columnRepresentables[indexPath.row - 1] |
||||
cells = cr.cellValues() |
||||
} |
||||
|
||||
var font: UIFont = UIFont.boldSystemFont(ofSize: FONTSIZE) |
||||
|
||||
var leftAnchor = cell.contentView.leftAnchor |
||||
for (index, value) in cells.enumerated() { |
||||
|
||||
if (indexPath.row > 0) { |
||||
font = self.columnDescriptors[index].number ? UIFont.monospacedDigitSystemFont(ofSize: FONTSIZE, weight: UIFont.Weight.regular) : UIFont.systemFont(ofSize: FONTSIZE) |
||||
} |
||||
|
||||
let label: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 30)) |
||||
label.textColor = UIColor.white |
||||
label.backgroundColor = UIColor.clear |
||||
label.text = value |
||||
label.font = font |
||||
label.textAlignment = self.columnDescriptors[index].number ? .right : .left |
||||
|
||||
cell.contentView.addSubview(label) |
||||
label.translatesAutoresizingMaskIntoConstraints = false |
||||
label.topAnchor.constraint(equalTo: cell.contentView.topAnchor).isActive = true |
||||
label.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor).isActive = true |
||||
label.leftAnchor.constraint(equalTo: leftAnchor).isActive = true |
||||
leftAnchor = label.rightAnchor |
||||
|
||||
label.backgroundColor = index.isMultiple(of: 2) ? UIColor.clear : UIColor(white: 0.9, alpha: 0.1) |
||||
|
||||
let multiplier: CGFloat = self.columnDescriptors[index].widthWeight / self.totalWidthWeigth |
||||
label.widthAnchor.constraint(equalTo: cell.contentView.widthAnchor, multiplier: multiplier).isActive = true |
||||
|
||||
} |
||||
return cell |
||||
} |
||||
|
||||
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { |
||||
return 36.0 |
||||
} |
||||
|
||||
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { |
||||
return 0.1 |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue