Skip to content

Commit e30e0dd

Browse files
authored
Merge pull request #22 from jeantimex/auto-height-support
Added auto height support.
2 parents fe763e0 + b7c5711 commit e30e0dd

File tree

4 files changed

+102
-18
lines changed

4 files changed

+102
-18
lines changed

ios-swift-collapsible-table-section.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
0A12C8C21D9C3AAF00D0BEE3 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A12C8C11D9C3AAF00D0BEE3 /* Extensions.swift */; };
11+
0A8465851F1DBC6E002CD874 /* CollapsibleTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A8465841F1DBC6E002CD874 /* CollapsibleTableViewCell.swift */; };
1112
0A908DEF1CFCAA9200470F33 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A908DEE1CFCAA9200470F33 /* AppDelegate.swift */; };
1213
0A908DF61CFCAA9200470F33 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0A908DF51CFCAA9200470F33 /* Assets.xcassets */; };
1314
0A908DF91CFCAA9200470F33 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0A908DF71CFCAA9200470F33 /* LaunchScreen.storyboard */; };
@@ -17,6 +18,7 @@
1718

1819
/* Begin PBXFileReference section */
1920
0A12C8C11D9C3AAF00D0BEE3 /* Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
21+
0A8465841F1DBC6E002CD874 /* CollapsibleTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollapsibleTableViewCell.swift; sourceTree = "<group>"; };
2022
0A908DEB1CFCAA9200470F33 /* ios-swift-collapsible-table-section.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "ios-swift-collapsible-table-section.app"; sourceTree = BUILT_PRODUCTS_DIR; };
2123
0A908DEE1CFCAA9200470F33 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
2224
0A908DF51CFCAA9200470F33 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
@@ -63,6 +65,7 @@
6365
0A908E001CFCAC7500470F33 /* CollapsibleTableViewController.swift */,
6466
0A908E021CFCB8D600470F33 /* CollapsibleTableViewHeader.swift */,
6567
0A12C8C11D9C3AAF00D0BEE3 /* Extensions.swift */,
68+
0A8465841F1DBC6E002CD874 /* CollapsibleTableViewCell.swift */,
6669
);
6770
path = "ios-swift-collapsible-table-section";
6871
sourceTree = "<group>";
@@ -140,6 +143,7 @@
140143
files = (
141144
0A908E011CFCAC7500470F33 /* CollapsibleTableViewController.swift in Sources */,
142145
0A908E031CFCB8D600470F33 /* CollapsibleTableViewHeader.swift in Sources */,
146+
0A8465851F1DBC6E002CD874 /* CollapsibleTableViewCell.swift in Sources */,
143147
0A908DEF1CFCAA9200470F33 /* AppDelegate.swift in Sources */,
144148
0A12C8C21D9C3AAF00D0BEE3 /* Extensions.swift in Sources */,
145149
);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// CollapsibleTableViewCell.swift
3+
// ios-swift-collapsible-table-section
4+
//
5+
// Created by Yong Su on 7/17/17.
6+
// Copyright © 2017 Yong Su. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
class CollapsibleTableViewCell: UITableViewCell {
12+
13+
let nameLabel = UILabel()
14+
let detailLabel = UILabel()
15+
16+
// MARK: Initalizers
17+
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
18+
super.init(style: style, reuseIdentifier: reuseIdentifier)
19+
20+
let marginGuide = contentView.layoutMarginsGuide
21+
22+
// configure nameLabel
23+
contentView.addSubview(nameLabel)
24+
nameLabel.translatesAutoresizingMaskIntoConstraints = false
25+
nameLabel.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor).isActive = true
26+
nameLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor).isActive = true
27+
nameLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor).isActive = true
28+
nameLabel.numberOfLines = 0
29+
nameLabel.font = UIFont.systemFont(ofSize: 16)
30+
31+
// configure detailLabel
32+
contentView.addSubview(detailLabel)
33+
detailLabel.lineBreakMode = .byWordWrapping
34+
detailLabel.translatesAutoresizingMaskIntoConstraints = false
35+
detailLabel.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor).isActive = true
36+
detailLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true
37+
detailLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor).isActive = true
38+
detailLabel.topAnchor.constraint(equalTo: nameLabel.bottomAnchor, constant: 5).isActive = true
39+
detailLabel.numberOfLines = 0
40+
detailLabel.font = UIFont.systemFont(ofSize: 12)
41+
detailLabel.textColor = UIColor.lightGray
42+
}
43+
44+
required init?(coder aDecoder: NSCoder) {
45+
fatalError("init(coder:) has not been implemented")
46+
}
47+
48+
}

ios-swift-collapsible-table-section/CollapsibleTableViewController.swift

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,22 @@ import UIKit
1111
//
1212
// MARK: - Section Data Structure
1313
//
14+
struct Item {
15+
var name: String
16+
var detail: String
17+
18+
init(name: String, detail: String) {
19+
self.name = name
20+
self.detail = detail
21+
}
22+
}
23+
1424
struct Section {
15-
var name: String!
16-
var items: [String]!
17-
var collapsed: Bool!
25+
var name: String
26+
var items: [Item]
27+
var collapsed: Bool
1828

19-
init(name: String, items: [String], collapsed: Bool = false) {
29+
init(name: String, items: [Item], collapsed: Bool = false) {
2030
self.name = name
2131
self.items = items
2232
self.collapsed = collapsed
@@ -33,14 +43,37 @@ class CollapsibleTableViewController: UITableViewController {
3343
override func viewDidLoad() {
3444
super.viewDidLoad()
3545

46+
// Auto resizing the height of the cell
47+
tableView.estimatedRowHeight = 44.0
48+
tableView.rowHeight = UITableViewAutomaticDimension
49+
3650
self.title = "Apple Products"
3751

3852
// Initialize the sections array
3953
// Here we have three sections: Mac, iPad, iPhone
4054
sections = [
41-
Section(name: "Mac", items: ["MacBook", "MacBook Air", "MacBook Pro", "iMac", "Mac Pro", "Mac mini", "Accessories", "OS X El Capitan"]),
42-
Section(name: "iPad", items: ["iPad Pro", "iPad Air 2", "iPad mini 4", "Accessories"]),
43-
Section(name: "iPhone", items: ["iPhone 6s", "iPhone 6", "iPhone SE", "Accessories"]),
55+
Section(name: "Mac", items: [
56+
Item(name: "MacBook", detail: "Apple's ultraportable laptop, trading portability for speed and connectivity."),
57+
Item(name: "MacBook Air", detail: "While the screen could be sharper, the updated 11-inch MacBook Air is a very light ultraportable that offers great performance and battery life for the price."),
58+
Item(name: "MacBook Pro", detail: "Retina Display The brightest, most colorful Mac notebook display ever. The display in the MacBook Pro is the best ever in a Mac notebook."),
59+
Item(name: "iMac", detail: "iMac combines enhanced performance with our best ever Retina display for the ultimate desktop experience in two sizes."),
60+
Item(name: "Mac Pro", detail: "Mac Pro is equipped with pro-level graphics, storage, expansion, processing power, and memory. It's built for creativity on an epic scale."),
61+
Item(name: "Mac mini", detail: "Mac mini is an affordable powerhouse that packs the entire Mac experience into a 7.7-inch-square frame."),
62+
Item(name: "OS X El Capitan", detail: "The twelfth major release of OS X (now named macOS)."),
63+
Item(name: "Accessories", detail: "")
64+
]),
65+
Section(name: "iPad", items: [
66+
Item(name: "iPad Pro", detail: "iPad Pro delivers epic power, in 12.9-inch and a new 10.5-inch size."),
67+
Item(name: "iPad Air 2", detail: "The second-generation iPad Air tablet computer designed, developed, and marketed by Apple Inc."),
68+
Item(name: "iPad mini 4", detail: "iPad mini 4 puts uncompromising performance and potential in your hand."),
69+
Item(name: "Accessories", detail: "")
70+
]),
71+
Section(name: "iPhone", items: [
72+
Item(name: "iPhone 6s", detail: "The iPhone 6S has a similar design to the 6 but updated hardware, including a strengthened chassis and upgraded system-on-chip, a 12-megapixel camera, improved fingerprint recognition sensor, and LTE Advanced support."),
73+
Item(name: "iPhone 6", detail: "The iPhone 6 and iPhone 6 Plus are smartphones designed and marketed by Apple Inc."),
74+
Item(name: "iPhone SE", detail: "The iPhone SE was received positively by critics, who noted its familiar form factor and design, improved hardware over previous 4-inch iPhone models, as well as its overall performance and battery life."),
75+
Item(name: "Accessories", detail: "")
76+
])
4477
]
4578
}
4679

@@ -56,20 +89,24 @@ extension CollapsibleTableViewController {
5689
}
5790

5891
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
59-
return sections[section].items.count
92+
return sections[section].collapsed ? 0 : sections[section].items.count
6093
}
6194

6295
// Cell
6396
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
64-
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell? ?? UITableViewCell(style: .default, reuseIdentifier: "cell")
97+
let cell: CollapsibleTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as? CollapsibleTableViewCell ??
98+
CollapsibleTableViewCell(style: .default, reuseIdentifier: "cell")
99+
100+
let item: Item = sections[(indexPath as NSIndexPath).section].items[(indexPath as NSIndexPath).row]
65101

66-
cell.textLabel?.text = sections[(indexPath as NSIndexPath).section].items[(indexPath as NSIndexPath).row]
102+
cell.nameLabel.text = item.name
103+
cell.detailLabel.text = item.detail
67104

68105
return cell
69106
}
70107

71108
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
72-
return sections[(indexPath as NSIndexPath).section].collapsed! ? 0 : 44.0
109+
return sections[(indexPath as NSIndexPath).section].collapsed ? 0 : UITableViewAutomaticDimension
73110
}
74111

75112
// Header
@@ -108,12 +145,7 @@ extension CollapsibleTableViewController: CollapsibleTableViewHeaderDelegate {
108145
sections[section].collapsed = collapsed
109146
header.setCollapsed(collapsed)
110147

111-
// Adjust the height of the rows inside the section
112-
tableView.beginUpdates()
113-
for i in 0 ..< sections[section].items.count {
114-
tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
115-
}
116-
tableView.endUpdates()
148+
tableView.reloadSections(NSIndexSet(index: section) as IndexSet, with: .automatic)
117149
}
118150

119151
}

ios-swift-collapsible-table-section/CollapsibleTableViewHeader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class CollapsibleTableViewHeader: UITableViewHeaderFooterView {
9797
//
9898
// Animate the arrow rotation (see Extensions.swf)
9999
//
100-
arrowLabel.rotate(collapsed ? 0.0 : CGFloat(M_PI_2))
100+
arrowLabel.rotate(collapsed ? 0.0 : .pi / 2)
101101
}
102102

103103
}

0 commit comments

Comments
 (0)