Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions DynamicHeightCollectionView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
TargetAttributes = {
BD85B938220DC08B00D33BF9 = {
CreatedOnToolsVersion = 10.1;
LastSwiftMigration = 1110;
};
};
};
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
14 changes: 9 additions & 5 deletions DynamicHeightCollectionView/Example/TableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class TableViewCell: UITableViewCell {
func configure(with arr: [String]) {
self.arr = arr
self.collectionView.reloadData()
self.collectionView.layoutIfNeeded()
// self.collectionView.layoutIfNeeded()
}
}

extension TableViewCell: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
extension TableViewCell: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.arr.count
}
Expand All @@ -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)
}
}
21 changes: 20 additions & 1 deletion DynamicHeightCollectionView/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -26,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
}
}