From e22ec03990f20f2328ebd9faaca80d129527563e Mon Sep 17 00:00:00 2001 From: Ivan Zinovyev Date: Wed, 12 Dec 2018 19:03:16 +0300 Subject: [PATCH] Add layout type --- Sources/ConfigurableCell.swift | 7 +++++++ Sources/Expandable.swift | 2 +- Sources/ExpandableCellHeightCalculator.swift | 2 +- Sources/LayoutType.swift | 7 +++++++ Sources/TableKit.swift | 3 ++- Sources/TableRow.swift | 4 ++++ Sources/UITableViewCell+Extensions.swift | 9 +++++++-- TableKit.xcodeproj/project.pbxproj | 4 ++++ 8 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 Sources/LayoutType.swift diff --git a/Sources/ConfigurableCell.swift b/Sources/ConfigurableCell.swift index 6454dc3..2c1fb4e 100644 --- a/Sources/ConfigurableCell.swift +++ b/Sources/ConfigurableCell.swift @@ -31,6 +31,8 @@ public protocol ConfigurableCell { static var defaultHeight: CGFloat? { get } + static var layoutType: LayoutType { get } + func configure(with _: CellData) func height(for _: CellData) -> CGFloat @@ -57,4 +59,9 @@ public extension ConfigurableCell where Self: UITableViewCell { static var defaultHeight: CGFloat? { return nil } + + static var layoutType: LayoutType { + return .auto + } + } diff --git a/Sources/Expandable.swift b/Sources/Expandable.swift index 51112de..a2ca692 100644 --- a/Sources/Expandable.swift +++ b/Sources/Expandable.swift @@ -47,7 +47,7 @@ extension Expandable where Self: UITableViewCell & ConfigurableCell { if let indexPath = indexPath, let tableDirector = (tableView?.delegate as? TableDirector), let cellHeightCalculator = tableDirector.rowHeightCalculator as? ExpandableCellHeightCalculator { - cellHeightCalculator.updateCached(height: height, for: indexPath) + cellHeightCalculator.updateCached(height: height(layoutType: Self.layoutType), for: indexPath) } } diff --git a/Sources/ExpandableCellHeightCalculator.swift b/Sources/ExpandableCellHeightCalculator.swift index a9bf8ee..08bbecf 100644 --- a/Sources/ExpandableCellHeightCalculator.swift +++ b/Sources/ExpandableCellHeightCalculator.swift @@ -39,7 +39,7 @@ public final class ExpandableCellHeightCalculator: RowHeightCalculator { row.configure(cell) cell.layoutIfNeeded() - let height = cell.height + let height = cell.height(layoutType: row.layoutType) cachedHeights[indexPath] = height return height } diff --git a/Sources/LayoutType.swift b/Sources/LayoutType.swift new file mode 100644 index 0000000..328ba4c --- /dev/null +++ b/Sources/LayoutType.swift @@ -0,0 +1,7 @@ +public enum LayoutType { + + case manual + + case auto + +} diff --git a/Sources/TableKit.swift b/Sources/TableKit.swift index 91031b3..fed5e9c 100644 --- a/Sources/TableKit.swift +++ b/Sources/TableKit.swift @@ -59,7 +59,8 @@ public protocol Row: RowConfigurable, RowActionable, RowHashable { var reuseIdentifier: String { get } var cellType: AnyClass { get } - + + var layoutType: LayoutType { get } var estimatedHeight: CGFloat? { get } var defaultHeight: CGFloat? { get } } diff --git a/Sources/TableRow.swift b/Sources/TableRow.swift index 1c5eef0..491015e 100644 --- a/Sources/TableRow.swift +++ b/Sources/TableRow.swift @@ -41,6 +41,10 @@ open class TableRow: Row where CellType: UITableView open var defaultHeight: CGFloat? { return CellType.defaultHeight } + + open var layoutType: LayoutType { + return CellType.layoutType + } open var cellType: AnyClass { return CellType.self diff --git a/Sources/UITableViewCell+Extensions.swift b/Sources/UITableViewCell+Extensions.swift index dee1042..b65055a 100644 --- a/Sources/UITableViewCell+Extensions.swift +++ b/Sources/UITableViewCell+Extensions.swift @@ -20,8 +20,13 @@ extension UITableViewCell { return indexPath } - public var height: CGFloat { - return contentView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height + public func height(layoutType: LayoutType) -> CGFloat { + switch layoutType { + case .auto: + return contentView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height + case .manual: + return contentView.subviews.map { $0.frame.maxY }.max() ?? 0 + } } } diff --git a/TableKit.xcodeproj/project.pbxproj b/TableKit.xcodeproj/project.pbxproj index de4d250..068fb5f 100644 --- a/TableKit.xcodeproj/project.pbxproj +++ b/TableKit.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ 3201E78821BE9EB2001DF9E7 /* Expandable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3201E78721BE9EB2001DF9E7 /* Expandable.swift */; }; 3201E78A21BE9ED4001DF9E7 /* ExpandableCellViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3201E78921BE9ED4001DF9E7 /* ExpandableCellViewModel.swift */; }; 320C5280218EB9A7004EAD1C /* AccurateCellHeightCalculator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 320C527F218EB9A7004EAD1C /* AccurateCellHeightCalculator.swift */; }; + 32BDFE9F21C167F400D0BBB4 /* LayoutType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32BDFE9E21C167F400D0BBB4 /* LayoutType.swift */; }; 50CF6E6B1D6704FE004746FF /* TableCellRegisterer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50CF6E6A1D6704FE004746FF /* TableCellRegisterer.swift */; }; 50E858581DB153F500A9AA55 /* TableKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50E858571DB153F500A9AA55 /* TableKit.swift */; }; DA9EA7AF1D0EC2C90021F650 /* ConfigurableCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA9EA7A61D0EC2C90021F650 /* ConfigurableCell.swift */; }; @@ -42,6 +43,7 @@ 3201E78721BE9EB2001DF9E7 /* Expandable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Expandable.swift; sourceTree = ""; }; 3201E78921BE9ED4001DF9E7 /* ExpandableCellViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExpandableCellViewModel.swift; sourceTree = ""; }; 320C527F218EB9A7004EAD1C /* AccurateCellHeightCalculator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccurateCellHeightCalculator.swift; sourceTree = ""; }; + 32BDFE9E21C167F400D0BBB4 /* LayoutType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LayoutType.swift; sourceTree = ""; }; 50CF6E6A1D6704FE004746FF /* TableCellRegisterer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableCellRegisterer.swift; sourceTree = ""; }; 50E858571DB153F500A9AA55 /* TableKit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableKit.swift; sourceTree = ""; }; DA9EA7561D0B679A0021F650 /* TableKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TableKit.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -115,6 +117,7 @@ 3201E78521BE9E25001DF9E7 /* UITableViewCell+Extensions.swift */, 3201E78721BE9EB2001DF9E7 /* Expandable.swift */, 3201E78921BE9ED4001DF9E7 /* ExpandableCellViewModel.swift */, + 32BDFE9E21C167F400D0BBB4 /* LayoutType.swift */, ); path = Sources; sourceTree = ""; @@ -256,6 +259,7 @@ 3201E78421BE9DE1001DF9E7 /* ExpandableCellHeightCalculator.swift in Sources */, DA9EA7B51D0EC2C90021F650 /* TableRowAction.swift in Sources */, DA9EA7B21D0EC2C90021F650 /* TableCellAction.swift in Sources */, + 32BDFE9F21C167F400D0BBB4 /* LayoutType.swift in Sources */, 3201E78621BE9E25001DF9E7 /* UITableViewCell+Extensions.swift in Sources */, DA9EA7B11D0EC2C90021F650 /* Operators.swift in Sources */, DA9EA7B41D0EC2C90021F650 /* TableRow.swift in Sources */,