CollapsibleTableSectionViewController
  • October 14, 2023

A Swift library that helps you collapse table view sections.

Features


  • Support collapsible sections in a table view
  • Collapse all the sections by default (configurable)
  • Keep only one section expanded (configurable)
  • Auto resize table view cell
  • Easy-to-use protocols for configuration

Requirements


  • iOS 9.0+
  • Xcode 10.0+
  • Swift 4.2

Installation


Manual

Just clone and add the following Swift files to your project:

  • CollapsibleTableSectionViewController.swfit
  • CollapsibleTableViewHeader.swift

CocoaPods

  • Make sure that you use latest stable Cocoapods version: pod --version
  • If not, update it: sudo gem install cocoapods
  • pod init in you project root dir
  • nano Podfile, add:

use_frameworks!
pod ‘CollapsibleTableSectionViewController’, ‘~> 2.0.1’

  • Save it: ctrl-xyenter
  • pod update
  • Open generated .xcworkspace
  • Don’t forget to import CollapsibleTableSectionViewController: import CollapsibleTableSectionViewController!

Carthage

  • nano Cartfile
  • put github "jeantimex/CollapsibleTableSectionViewController" ~> 2.0.1 into Cartfile
  • Save it: ctrl-xyenter
  • Run carthage update
  • Copy CollapsibleTableSectionViewController.framework from Carthage/Build/iOS to your project
  • Make sure that CollapsibleTableSectionViewController is added in Embedded Binaries section of your target (or else you will get dyld library not loaded referenced from ... reason image not found error)
  • Add import CollapsibleTableSectionViewController on top of your view controller’s code

Usage


Step 1. Subclass CollapsibleTableSectionViewController

import CollapsibleTableSectionViewController

class ViewController: CollapsibleTableSectionViewController { … }

Step 2. Conforms to the CollapsibleTableSectionDelegate protocol

extension ViewController: CollapsibleTableSectionDelegate { … }

CollapsibleTableSectionDelegate Protocol


Most of the protocol methods are optional and they are very similar to UITableViewDataSource and UITableViewDelegate, here is a list of the available protocol methods:

1. optional func numberOfSections(_tableview: UITableView) – >Int

Asks the data source to return the number of sections in the table view. Default is 1.

extension ViewController: CollapsibleTableSectionDelegate {
func numberOfSections(_ tableView: UITableView) -> Int {
return 10
}
}

2. optional func collapsibleTableView(_tableview: UITableView, numberOfRowsInSection section: Int) – >Int

Returns the number of rows (table cells) in a specified section. Default is 0.

extension ViewController: CollapsibleTableSectionDelegate {
func collapsibleTableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
}

3. optional func collapsibleTableView(_tableView: UITableView, cellForRowAt indexPath: IndexPath) – >UITableViewCell

Returns the table cell at the specified index path. You can also use your custom cells, see our example projects for more details.

extension ViewController: CollapsibleTableSectionDelegate {
func collapsibleTableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: “Cell”) as UITableViewCell? ?? UITableViewCell(style: .default, reuseIdentifier: “Cell”)
cell.textLabel?.text = “Cell Text”
return cell
}
}

4. optional func shouldCollapsibleByDefault(_tableView: UITableView) – >Bool

Return true if you would like collapse all the sections when the table is loaded. Default is false.

5. optional func shouldCollapseOthers(_tableView: UItableView) – >Bool

Return true if you would like to keep only one extended section (like accordion style). Default is false.

6. optional func collapsibleTableView(_tableView: UITableView,titleForHeaderInSection section: Int) – >String?

The title for each section. Default is nil.

7. optional func collapsibleTableView(_tableView: UITableView, didSelectRowAt indexPath: IndexPath)

Tells the delegate that the specified row is now selected.

Examples


Run the Examples project in this repo and you will find the following demos that help you get up and running:

  1. Basic: The minimal working example
  2. Custom Cell: Implement a custom cell programmatically
  3. Collapse By Default: All sections are collapsed by default
  4. Collapse Others: Accordion-style table view that only keeps one section expanded at a time

For more details of how to implement collapsible table sections using Swift, please checkout this repo for more information: https://github.com/jeantimex/ios-swift-collapsible-table-section.

GitHub


View Github

#autolayout #cocoa #cocoapod #cocoapods #cocoatouch #collapsiblesections #collection #collectionkit #collectionview #collectionviewcell #collectionviewlayout #customlayout #dynamic #expandabletableview #flowlayout #hashtag #hashtags #ibinspectable #instagram #instagramanimation #ios #iosanimation #iosdevelopment #iossdk #iosswift #iosthirdparty #lightboxalgorithm #objectivec #swift #swift42 #swiftanimation #swiftcollection #swiftimage #swiftlibrary #swiftpackagemanager #swiftui #swiftuicomponents #table #tableview #tableviewcell #taglistview #tags #tagsview #uicollectionview #uicollectionviewanimation #uicollectionviewcell #uicollectionviewflowlayout #uicollectionviewlayout #uitableview #uitableviewcell #uitableviewcontroller #uitableviewcontrollercocoapods #xcode
YOU MIGHT ALSO LIKE...
CameraBackground

Features Both front and back camera supported. Flash modes: auto, on, off. Countdown timer. Tap to focus. Pinch to zoom. Usage  

DKCamera

Description A light weight & simple & easy camera for iOS by Swift. It uses CoreMotion framework to detect device orientation, so ...

HorizonSDK-iOS

Horizon SDK is a state of the art real-time video recording / photo shooting iOS library. Some of the features ...

LLSimpleCamera

LLSimpleCamera: A simple customizable camera - video recorder control LLSimpleCamera is a library for creating a customized camera - video ...

RSBarcodes_Swift

RSBarcodes allows you to read 1D and 2D barcodes using the metadata scanning capabilities introduced with iOS 7 and generate ...