Skip to content

Commit e0bb262

Browse files
committed
Fixed the auto height bug by optimizing the reloading of sections.
1 parent 6e49241 commit e0bb262

File tree

2 files changed

+3
-7
lines changed

2 files changed

+3
-7
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class CollapsibleTableViewCell: UITableViewCell {
3030

3131
// configure detailLabel
3232
contentView.addSubview(detailLabel)
33+
detailLabel.lineBreakMode = .byWordWrapping
3334
detailLabel.translatesAutoresizingMaskIntoConstraints = false
3435
detailLabel.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor).isActive = true
3536
detailLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ extension CollapsibleTableViewController {
8989
}
9090

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

9595
// Cell
@@ -145,12 +145,7 @@ extension CollapsibleTableViewController: CollapsibleTableViewHeaderDelegate {
145145
sections[section].collapsed = collapsed
146146
header.setCollapsed(collapsed)
147147

148-
// Adjust the height of the rows inside the section
149-
tableView.beginUpdates()
150-
for i in 0 ..< sections[section].items.count {
151-
tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
152-
}
153-
tableView.endUpdates()
148+
tableView.reloadSections(NSIndexSet(index: section) as IndexSet, with: .automatic)
154149
}
155150

156151
}

0 commit comments

Comments
 (0)