|
| 1 | +// |
| 2 | +// _StackScrollView.swift |
| 3 | +// StackScrollView |
| 4 | +// |
| 5 | +// Created by muukii on 2016/10/26. |
| 6 | +// Copyright © 2016 muukii. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +public protocol _StackScrollViewCellType { |
| 12 | + |
| 13 | +} |
| 14 | + |
| 15 | +open class BetaStackScrollView: UIView, UITableViewDataSource, UITableViewDelegate { |
| 16 | + |
| 17 | + public override init(frame: CGRect) { |
| 18 | + |
| 19 | + super.init(frame: frame) |
| 20 | + setup() |
| 21 | + } |
| 22 | + |
| 23 | + public required init?(coder aDecoder: NSCoder) { |
| 24 | + |
| 25 | + super.init(coder: aDecoder) |
| 26 | + setup() |
| 27 | + } |
| 28 | + |
| 29 | + open func setup() { |
| 30 | + |
| 31 | + tableView.backgroundColor = UIColor.clear |
| 32 | + tableView.autoresizingMask = [.flexibleHeight, .flexibleWidth] |
| 33 | + tableView.frame = bounds |
| 34 | + |
| 35 | + addSubview(tableView) |
| 36 | + } |
| 37 | + |
| 38 | + open override func didMoveToSuperview() { |
| 39 | + super.didMoveToSuperview() |
| 40 | + if superview != nil { |
| 41 | + tableView.delegate = self |
| 42 | + tableView.dataSource = self |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + open func append(view: UIView, animated: Bool) { |
| 47 | + |
| 48 | + let cell = UITableViewCell(frame: .zero) |
| 49 | + let contentView = cell.contentView |
| 50 | + view.translatesAutoresizingMaskIntoConstraints = false |
| 51 | + contentView.addSubview(view) |
| 52 | + NSLayoutConstraint.activate([ |
| 53 | + NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .top, multiplier: 1, constant: 0), |
| 54 | + NSLayoutConstraint(item: view, attribute: .right, relatedBy: .equal, toItem: contentView, attribute: .right, multiplier: 1, constant: 0), |
| 55 | + NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: contentView, attribute: .bottom, multiplier: 1, constant: 0), |
| 56 | + NSLayoutConstraint(item: view, attribute: .left, relatedBy: .equal, toItem: contentView, attribute: .left, multiplier: 1, constant: 0), |
| 57 | + ]) |
| 58 | + cells.append(cell) |
| 59 | + |
| 60 | + // TODO: Improve performance, animated |
| 61 | + tableView.reloadData() |
| 62 | + } |
| 63 | + |
| 64 | + open func append(views: [UIView], animated: Bool) { |
| 65 | + |
| 66 | + let _cells = views.map { view -> UITableViewCell in |
| 67 | + let cell = UITableViewCell(frame: .zero) |
| 68 | + let contentView = cell.contentView |
| 69 | + view.translatesAutoresizingMaskIntoConstraints = false |
| 70 | + contentView.addSubview(view) |
| 71 | + NSLayoutConstraint.activate([ |
| 72 | + NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .top, multiplier: 1, constant: 0), |
| 73 | + NSLayoutConstraint(item: view, attribute: .right, relatedBy: .equal, toItem: contentView, attribute: .right, multiplier: 1, constant: 0), |
| 74 | + NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: contentView, attribute: .bottom, multiplier: 1, constant: 0), |
| 75 | + NSLayoutConstraint(item: view, attribute: .left, relatedBy: .equal, toItem: contentView, attribute: .left, multiplier: 1, constant: 0), |
| 76 | + ]) |
| 77 | + return cell |
| 78 | + } |
| 79 | + |
| 80 | + cells += _cells |
| 81 | + // TODO: Improve performance, animated |
| 82 | + tableView.reloadData() |
| 83 | + } |
| 84 | + |
| 85 | + open func remove(view: UIView, animated: Bool) { |
| 86 | + |
| 87 | + if let index = cells.flatMap({ $0.contentView.subviews.first }).index(of: view) { |
| 88 | + cells.remove(at: index) |
| 89 | + } |
| 90 | + |
| 91 | + // TODO: Improve performance, animated |
| 92 | + tableView.reloadData() |
| 93 | + } |
| 94 | + |
| 95 | + public var cells: [UITableViewCell] = [] |
| 96 | + |
| 97 | + private let tableView = UITableView(frame: .zero, style: .plain) |
| 98 | + |
| 99 | + // MARK: - UITableViewDataSource |
| 100 | + |
| 101 | + public func numberOfSections(in tableView: UITableView) -> Int { |
| 102 | + return 1 |
| 103 | + } |
| 104 | + |
| 105 | + public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
| 106 | + |
| 107 | + return cells.count |
| 108 | + } |
| 109 | + |
| 110 | + public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { |
| 111 | + |
| 112 | + return cells[indexPath.row] |
| 113 | + } |
| 114 | + |
| 115 | + public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { |
| 116 | + |
| 117 | + let cell = cells[indexPath.row] |
| 118 | + |
| 119 | + let contentView = cell.contentView |
| 120 | + let width = NSLayoutConstraint(item: contentView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: tableView.bounds.width) |
| 121 | + |
| 122 | + NSLayoutConstraint.activate([width]) |
| 123 | + |
| 124 | + let size = contentView.systemLayoutSizeFitting(UILayoutFittingCompressedSize) |
| 125 | + |
| 126 | + NSLayoutConstraint.deactivate([width]) |
| 127 | + |
| 128 | + return size.height |
| 129 | + } |
| 130 | +} |
0 commit comments