You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
4.2 KiB
105 lines
4.2 KiB
//
|
|
// 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]
|
|
var columnDescriptors: [ColumnDescriptor] // = T.columnDescriptors()
|
|
var totalWidthWeigth: CGFloat = 0.0
|
|
fileprivate var _maxRows: Int?
|
|
|
|
init(array: [T], maxRows: Int? = nil) {
|
|
self.columnRepresentables = array
|
|
self.columnDescriptors = array.first?.columnDescriptors() ?? []
|
|
self.totalWidthWeigth = self.columnDescriptors.map { $0.widthWeight }.reduce(0, +)
|
|
if let max = maxRows, max > columnRepresentables.count {
|
|
self._maxRows = nil
|
|
} else {
|
|
self._maxRows = maxRows
|
|
}
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
// print("numberOfRowsInSection = \(self.columnRepresentables.count)")
|
|
if let max = self._maxRows {
|
|
return 1 + max
|
|
} else {
|
|
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 = UITableViewCell()
|
|
cell.backgroundColor = UIColor.clear
|
|
|
|
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 = Fonts.bold
|
|
|
|
var leftAnchor = cell.contentView.leftAnchor
|
|
for (index, value) in cells.enumerated() {
|
|
|
|
if (indexPath.row > 0) {
|
|
font = self.columnDescriptors[index].number ? Fonts.monospaced : Fonts.regular
|
|
}
|
|
|
|
let labelContainer: UIView = UIView()
|
|
cell.contentView.addSubview(labelContainer)
|
|
labelContainer.translatesAutoresizingMaskIntoConstraints = false
|
|
labelContainer.topAnchor.constraint(equalTo: cell.contentView.topAnchor).isActive = true
|
|
labelContainer.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor).isActive = true
|
|
let leftPadding: CGFloat = (index == 0) ? 0.0 : 0.0 //16.0
|
|
labelContainer.leftAnchor.constraint(equalTo: leftAnchor, constant: leftPadding).isActive = true
|
|
leftAnchor = labelContainer.rightAnchor
|
|
|
|
// labelContainer.backgroundColor = index.isMultiple(of: 2) ? UIColor.clear : UIColor(white: 0.9, alpha: 0.1)
|
|
labelContainer.backgroundColor = UIColor.clear
|
|
|
|
let multiplier: CGFloat = self.columnDescriptors[index].widthWeight / self.totalWidthWeigth
|
|
labelContainer.widthAnchor.constraint(equalTo: cell.contentView.widthAnchor, multiplier: multiplier).isActive = true
|
|
|
|
let label: UILabel = UILabel(frame: CGRect.zero)
|
|
label.textColor = UIColor.white
|
|
label.backgroundColor = UIColor.clear
|
|
label.text = value
|
|
label.font = font
|
|
label.textAlignment = self.columnDescriptors[index].number ? .right : .left
|
|
labelContainer.addSubview(label)
|
|
|
|
label.translatesAutoresizingMaskIntoConstraints = false
|
|
label.topAnchor.constraint(equalTo: labelContainer.topAnchor).isActive = true
|
|
label.bottomAnchor.constraint(equalTo: labelContainer.bottomAnchor).isActive = true
|
|
label.leftAnchor.constraint(equalTo: labelContainer.leftAnchor, constant: 8.0).isActive = true
|
|
label.rightAnchor.constraint(equalTo: labelContainer.rightAnchor, constant: -8.0).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
|
|
}
|
|
|
|
}
|
|
|