Skip to content

Commit c1cb7b6

Browse files
authored
Merge pull request #26 from jeantimex/simplify-the-heightForRowAtIndexPath
Simplify the heightForRowAtIndexPath.
2 parents 5faa955 + b2bc4d1 commit c1cb7b6

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

README.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ override func viewDidLoad() {
4949

5050
...
5151
}
52+
53+
override func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
54+
return UITableViewAutomaticDimension
55+
}
5256
```
5357

5458
#### Step 3. The Section Header ####
@@ -146,15 +150,15 @@ override func numberOfSectionsInTableView(in tableView: UITableView) -> Int {
146150
}
147151
```
148152

149-
and the number of rows in each section is:
153+
Here is the key ingredient of implementing the collapsible table section, if the section is collapsed, then that section should not have any row:
150154

151155
```swift
152156
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
153157
return sections[section].collapsed ? 0 : sections[section].items.count
154158
}
155159
```
156160

157-
Noticed that we don't need to render any cell for the collapsed section, this can improve the performance a lot if there are lots of cells in that section.
161+
Noticed that we don't need to render any cell for the collapsed section, this can improve the performance a lot if there are tons of cells in that section.
158162

159163
Next, we use tableView's viewForHeaderInSection function to hook up our custom header:
160164

@@ -187,15 +191,7 @@ In the above code, we use the plain `UITableViewCell`, if you would like to see
187191

188192
#### Step 5. How to Toggle Collapse and Expand ####
189193

190-
The idea is really simple, if a section's `collapsed` property is `true`, we set the height of the rows inside that section to be `0`, otherwise `UITableViewAutomaticDimension`!
191-
192-
```swift
193-
override func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
194-
return sections[indexPath.section].collapsed ? 0 : UITableViewAutomaticDimension
195-
}
196-
```
197-
198-
And here is the toggle function:
194+
The idea is pretty starightforward, reverse the `collapsed` flag for the section and tell the tableView to reload that section:
199195

200196
```swift
201197
extension CollapsibleTableViewController: CollapsibleTableViewHeaderDelegate {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extension CollapsibleTableViewController {
5454
}
5555

5656
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
57-
return sections[indexPath.section].collapsed ? 0 : UITableViewAutomaticDimension
57+
return UITableViewAutomaticDimension
5858
}
5959

6060
// Header

0 commit comments

Comments
 (0)