From 18b240030b865db3c6c99ce1ee480d43e4e03bca Mon Sep 17 00:00:00 2001 From: AmrAngry Date: Wed, 30 Oct 2019 17:10:16 +0400 Subject: [PATCH 1/5] upgrade to swift 5 --- DynamicHeightCollectionView.xcodeproj/project.pbxproj | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/DynamicHeightCollectionView.xcodeproj/project.pbxproj b/DynamicHeightCollectionView.xcodeproj/project.pbxproj index 250352d..a2e3936 100644 --- a/DynamicHeightCollectionView.xcodeproj/project.pbxproj +++ b/DynamicHeightCollectionView.xcodeproj/project.pbxproj @@ -116,6 +116,7 @@ TargetAttributes = { BD85B938220DC08B00D33BF9 = { CreatedOnToolsVersion = 10.1; + LastSwiftMigration = 1110; }; }; }; @@ -316,7 +317,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.infoedge.DynamicHeightCollectionView; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = "99acres Generic"; - SWIFT_VERSION = 4.2; + SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; @@ -335,7 +336,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.infoedge.DynamicHeightCollectionView; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = "99acres Generic"; - SWIFT_VERSION = 4.2; + SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Release; From d1d89ef9df28a52acb6c7fd3aefb6f1f28543ae6 Mon Sep 17 00:00:00 2001 From: AmrAngry Date: Wed, 30 Oct 2019 17:13:37 +0400 Subject: [PATCH 2/5] formatting --- .../Example/ViewController.swift | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/DynamicHeightCollectionView/Example/ViewController.swift b/DynamicHeightCollectionView/Example/ViewController.swift index 640a1dc..5466670 100644 --- a/DynamicHeightCollectionView/Example/ViewController.swift +++ b/DynamicHeightCollectionView/Example/ViewController.swift @@ -9,9 +9,27 @@ import UIKit class ViewController: UIViewController { + @IBOutlet weak var tableView: UITableView! - var arr = ["Basic Operators", "Strings and Characters", "Collection Types", "Control Flow", "Structures and Classes", "Optional Chaining", "Closures", "Automatic Reference Counting", "Advanced Operators", "Access Control", "Memory Safety", "Generics", "Protocols", "Extensions", "Type Casting", "Nested Types", "Error Handling", "Deinitialization"] + var arr = ["Basic Operators", + "Strings and Characters", + "Collection Types", + "Control Flow", + "Structures and Classes", + "Optional Chaining", + "Closures", + "Automatic Reference Counting", + "Advanced Operators", + "Access Control", + "Memory Safety", + "Generics", + "Protocols", + "Extensions", + "Type Casting", + "Nested Types", + "Error Handling", + "Deinitialization"] override func viewDidLoad() { super.viewDidLoad() From ef781e089e3becf58d22afbfce9b8382985b03b4 Mon Sep 17 00:00:00 2001 From: AmrAngry Date: Wed, 30 Oct 2019 17:14:31 +0400 Subject: [PATCH 3/5] comment unneeded layout call --- DynamicHeightCollectionView/Example/TableViewCell.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DynamicHeightCollectionView/Example/TableViewCell.swift b/DynamicHeightCollectionView/Example/TableViewCell.swift index 79b3ca3..28432c6 100644 --- a/DynamicHeightCollectionView/Example/TableViewCell.swift +++ b/DynamicHeightCollectionView/Example/TableViewCell.swift @@ -15,7 +15,7 @@ class TableViewCell: UITableViewCell { func configure(with arr: [String]) { self.arr = arr self.collectionView.reloadData() - self.collectionView.layoutIfNeeded() + // self.collectionView.layoutIfNeeded() } } From 3cf70ef95a977300ae9f5e267fc87d533565ee0c Mon Sep 17 00:00:00 2001 From: AmrAngry Date: Wed, 30 Oct 2019 17:14:51 +0400 Subject: [PATCH 4/5] use layout for table view cell --- DynamicHeightCollectionView/Example/ViewController.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/DynamicHeightCollectionView/Example/ViewController.swift b/DynamicHeightCollectionView/Example/ViewController.swift index 5466670..3c996a7 100644 --- a/DynamicHeightCollectionView/Example/ViewController.swift +++ b/DynamicHeightCollectionView/Example/ViewController.swift @@ -44,6 +44,7 @@ extension ViewController: UITableViewDataSource, UITableViewDelegate { func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell cell.configure(with: self.arr) + cell.layoutIfNeeded() return cell } } From a7e1a9b3d09af9c585fbc5e79403a769a931e3f0 Mon Sep 17 00:00:00 2001 From: AmrAngry Date: Wed, 30 Oct 2019 17:26:50 +0400 Subject: [PATCH 5/5] segregate protocols --- .../Example/TableViewCell.swift | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/DynamicHeightCollectionView/Example/TableViewCell.swift b/DynamicHeightCollectionView/Example/TableViewCell.swift index 28432c6..c5f9ca5 100644 --- a/DynamicHeightCollectionView/Example/TableViewCell.swift +++ b/DynamicHeightCollectionView/Example/TableViewCell.swift @@ -19,7 +19,7 @@ class TableViewCell: UITableViewCell { } } -extension TableViewCell: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { +extension TableViewCell: UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.arr.count } @@ -29,10 +29,14 @@ extension TableViewCell: UICollectionViewDataSource, UICollectionViewDelegateFlo cell.textLabel.text = self.arr[indexPath.row] return cell } - +} + +extension TableViewCell: UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let text = self.arr[indexPath.row] - let cellWidth = text.size(withAttributes:[.font: UIFont.systemFont(ofSize:12.0)]).width + 30.0 - return CGSize(width: cellWidth, height: 30.0) + let textSize = text.size(withAttributes:[.font: UIFont.systemFont(ofSize:12.0)]) + let cellHeight = textSize.height + 20.0 + let cellWidth = textSize.width + 30.0 + return CGSize(width: cellWidth, height: cellHeight) } }