You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+75-39Lines changed: 75 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
1
# How to Implement Collapsible Table Section in iOS
2
2
3
-
:iphone: A simple iOS swift project demonstrates how to implement collapsible table section programmatically, that is no main storyboard, no XIB, no need to register nib, just purely Swift!
3
+
A simple iOS swift project demonstrates how to implement collapsible table section programmatically, that is no main storyboard, no XIB, no need to register nib, just pure Swift!
4
+
5
+
In this project, the table view automatically resizes the height of the rows to fit the content in each cell.
4
6
5
7

6
8
@@ -14,28 +16,63 @@ Let's say we have the following data that is grouped into different sections, ea
Ignore this step if your cells have a fixed height.
74
+
75
+
#### Step 3. The Section Header ####
39
76
40
77
According to [Apple API reference](https://developer.apple.com/reference/uikit/uitableviewheaderfooterview), we should use `UITableViewHeaderFooterView`. Let's subclass it and implement the section header `CollapsibleTableViewHeader`:
41
78
@@ -83,7 +120,7 @@ class CollapsibleTableViewHeader: UITableViewHeaderFooterView {
83
120
84
121
funcsetCollapsed(_collapsed: Bool) {
85
122
// Animate the arrow rotation (see Extensions.swf)
We use tableView's viewForHeaderInSection function to hook up our custom header:
191
+
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.
192
+
193
+
Next, we use tableView's viewForHeaderInSection function to hook up our custom header:
let item: Item = sections[(indexPath as NSIndexPath).section].items[(indexPath as NSIndexPath).row]
217
+
218
+
cell.nameLabel.text= item.name
219
+
cell.detailLabel.text= item.detail
220
+
221
+
return cell
180
222
}
181
223
```
182
224
183
-
#### Step 4. How to Toggle Collapse and Expand ####
225
+
Of course you can use a plain `UITableViewCell`, our `CollapsibleTableViewCell` is a subclass of `UITableViewCell` that adds the name and detail labels, and the most important thing is that it supports autosizing feature, the key is to setup the autolayout constrains properly, please refer to the source code for more details.
226
+
227
+
#### Step 5. How to Toggle Collapse and Expand ####
184
228
185
-
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 `44.0`!
229
+
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`!
//Adjust the height of the rows inside the section
205
-
tableView.beginUpdates()
206
-
for i in0..< sections[section].items.count {
207
-
tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: i, inSection: section)], withRowAnimation: .Automatic)
208
-
}
209
-
tableView.endUpdates()
210
-
}
244
+
//Toggle collapse
245
+
sections[section].collapsed= collapsed
246
+
header.setCollapsed(collapsed)
247
+
248
+
// Reload the whole section
249
+
tableView.reloadSections(NSIndexSet(index: section) as IndexSet, with: .automatic)
250
+
}
211
251
}
212
252
```
213
253
214
-
Noticed that we don't lazily just reload the whole section, we only reload the rows inside that section, so that we won't see the refresh of the section header, and most importantly it will allow us to animate anything in the section header smoothly, i.e., rotate the arrow label, change the background etc.
254
+
After the sections get reloaded, the number of cells in that section will be recalculated and redrawn.
215
255
216
256
That's it, please refer to the source code and see the detailed implementation.
217
257
218
-
### What's coming next? ###
219
-
220
-
- Custom tableview cell with auto height support, see branch https://github.com/jeantimex/ios-swift-collapsible-table-section/pull/22.
221
-
222
258
### More Collapsible Demo ###
223
259
224
260
Sometimes you might want to implement the collapsible cells in a grouped-style table, I have a separate demo at [https://github.com/jeantimex/ios-swift-collapsible-table-section-in-grouped-section](https://github.com/jeantimex/ios-swift-collapsible-table-section-in-grouped-section). The implementation is pretty much the same but slightly different.
0 commit comments