Files
Juanpe Catalán d8436f79f6 Codebase refactor (#442)
* files reorganization (#432)

* fix podspec

* only include swift files

* Fix broken readme paths (#433)

* update links

* fix french link

* split skeleton debug to public and internal methods (#434)

* Add build phase to execute SwiftLint (#435)

* update tvOS tests

* Refactor Extensions folder (#436)

* Reorganize SkeletonAppearance (#437)

* reorganize helpers and builders folders (#438)

* Refactor flow and multilines (#439)

* Reorganize collections folder (#440)

* reorganize collections folder

* update podspec

* Refactor SkeletonView facade (#441)

* reorganize collections folder

* update podspec

* refactor skeletonview facade

* fix conflict

* update iOS example

* fix github workflows

* update tvOS example project build settings
2021-08-19 14:45:33 +02:00

90 lines
3.0 KiB
Swift

//
// UITableView+CollectionSkeleton.swift
// SkeletonView-iOS
//
// Created by Juanpe Catalán on 02/02/2018.
// Copyright © 2018 SkeletonView. All rights reserved.
//
import UIKit
extension UITableView: CollectionSkeleton {
var estimatedNumberOfRows: Int {
return Int(ceil(frame.height / rowHeight))
}
var skeletonDataSource: SkeletonCollectionDataSource? {
get { return ao_get(pkey: &CollectionAssociatedKeys.dummyDataSource) as? SkeletonCollectionDataSource }
set {
ao_setOptional(newValue, pkey: &CollectionAssociatedKeys.dummyDataSource)
self.dataSource = newValue
}
}
var skeletonDelegate: SkeletonCollectionDelegate? {
get { return ao_get(pkey: &CollectionAssociatedKeys.dummyDelegate) as? SkeletonCollectionDelegate }
set {
ao_setOptional(newValue, pkey: &CollectionAssociatedKeys.dummyDelegate)
self.delegate = newValue
}
}
func addDummyDataSource() {
guard let originalDataSource = self.dataSource as? SkeletonTableViewDataSource,
!(originalDataSource is SkeletonCollectionDataSource)
else { return }
let calculatedRowHeight = calculateRowHeight()
let dataSource = SkeletonCollectionDataSource(tableViewDataSource: originalDataSource,
rowHeight: rowHeight,
originalRowHeight: self.rowHeight)
rowHeight = calculatedRowHeight
self.skeletonDataSource = dataSource
if let originalDelegate = self.delegate as? SkeletonTableViewDelegate,
!(originalDelegate is SkeletonCollectionDelegate) {
let delegate = SkeletonCollectionDelegate(tableViewDelegate: originalDelegate)
self.skeletonDelegate = delegate
}
reloadData()
}
func updateDummyDataSource() {
if (dataSource as? SkeletonCollectionDataSource) != nil {
reloadData()
} else {
addDummyDataSource()
}
}
func removeDummyDataSource(reloadAfter: Bool) {
guard let dataSource = self.dataSource as? SkeletonCollectionDataSource else { return }
restoreRowHeight()
self.skeletonDataSource = nil
self.dataSource = dataSource.originalTableViewDataSource
if let delegate = self.delegate as? SkeletonCollectionDelegate {
self.skeletonDelegate = nil
self.delegate = delegate.originalTableViewDelegate
}
if reloadAfter { self.reloadData() }
}
}
private extension UITableView {
func restoreRowHeight() {
guard let dataSource = self.dataSource as? SkeletonCollectionDataSource else { return }
rowHeight = dataSource.originalRowHeight
}
func calculateRowHeight() -> CGFloat {
guard rowHeight == UITableView.automaticDimension else { return rowHeight }
return estimatedRowHeight
}
}