• Stars
    star
    350
  • Rank 121,229 (Top 3 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🎉 Swift library to support collapsible sections in a table view.

CollapsibleTableSectionViewController

Platform Swift 4.2 CocoaPods Carthage compatible Build codecov.io License Donate

A Swift library that helps you collapse table view sections.

cover

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-x, y, enter
  • 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-x, y, enter
  • 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 shouldCollapseByDefault(_ 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.

Contribution

Anyone who would like to contribute to the project is more than welcome.

  • Fork this repo
  • Make your changes
  • Submit pull request

License

MIT License

Copyright (c) 2017 Yong Su @jeantimex

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

ios-swift-collapsible-table-section

📱 A simple iOS Swift project demonstrates how to implement collapsible table section.
Swift
1,209
star
2

javascript-problems-and-solutions

🔱 A collection of JavaScript problems and solutions for studying algorithms.
JavaScript
484
star
3

react-sublime-snippet

Save time in writing React codes
102
star
4

ios-swift-collapsible-table-section-in-grouped-section

A simple demo that demonstrates how to implement collapsible table sections in grouped table view.
Swift
86
star
5

klotski

🔸 The JavaScript algorithm for solving klotski game.
JavaScript
38
star
6

five-in-a-row

A classic Chinese board game built with React and Socket.io.
JavaScript
36
star
7

react-webpack-boilerplate

React Webpack Boilerplate
JavaScript
22
star
8

apple-watch-ui

Implement Apple Watch UI with AngularJS
JavaScript
18
star
9

hua-rong-dao-html

AngularJS HTML5 puzzle game 华容道
JavaScript
17
star
10

SwiftyLib

A CocoaPods library written in Swift
Ruby
12
star
11

ios-swift-highlight-searchbar-placeholder

A demo shows how to highlight certain text in the search bar.
Swift
10
star
12

swift-material-uitextfield

Another material-style UITextField written in Swift.
Swift
8
star
13

generator-react-webpack-scaffold

A Yeoman generator that scaffolds React application with Webpack.
JavaScript
8
star
14

react-webapp-boilerplate

🚀 React Webapp Boilerplate demonstrates how to scaffold a web application using React, Webpack and Jest.
JavaScript
8
star
15

how-to-implement-promise

How to implement a Promise that is Promises/A+ compliant using vanilla JavaScript.
JavaScript
5
star
16

slush-html5-app

Scaffold your next HTML5 app with webpack and webpack dev server.
JavaScript
2
star
17

react-electron-boilerplate

🚀 React Electron Boilerplate demonstrates how to scaffold a desktop application using React and Electron.
JavaScript
2
star
18

closure-playground

A playground for learning closure
1
star
19

threejs-globe

Created with CodeSandbox
JavaScript
1
star
20

SwiftyCalculator

A Cocoapods calculator library written in Swift
Swift
1
star
21

react-advanced-markers

For debugging an issue with Advanced Markers and React
JavaScript
1
star
22

generator-typescript-webapp

This Yeoman generator helps you scaffold your next web application with TypeScript and Webpack.
JavaScript
1
star
23

traffic-old

A road traffic simulation based on the intelligent driver model (IDW)
JavaScript
1
star
24

react-webpack-code-splitting

🔱 A demo of how to split the code for a React Webpack application.
JavaScript
1
star
25

calculator

A UMD calculator
JavaScript
1
star
26

chat-app

A simple chat app built with Objective-C and MultipeerConnectivity
Objective-C
1
star
27

generator-html-app

Scaffold your next HTML5 app with webpack and webpack dev server.
JavaScript
1
star
28

generator-web-application

Scaffold your next web application for prototyping and production.
JavaScript
1
star
29

iBlog

iOS blog app
Objective-C
1
star
30

webgl2-study

Stores all the codes that I learn about WebGL2
TypeScript
1
star
31

dotfiles

My iTerm2, tmux and neovim setup
Lua
1
star
32

tensorflow-study

My TensorFlow projects
TypeScript
1
star
33

umd-lib

Another JavaScript UMD library
JavaScript
1
star
34

flex-badge-button

Extends Flex button component to have iOS-like badge icon.
1
star
35

laioffer-demos

Demo projects for Laioffer Nodejs class.
JavaScript
1
star
36

slush-webpack-html

🚀 Scaffold your next html5 app with webpack and webpack dev server.
JavaScript
1
star
37

material-color-palettes

Material color palettes
JavaScript
1
star
38

genetic-flappy-bird

JavaScript
1
star
39

swift-scroll-pages-with-transition

A demo shows how to implement transition when scrolling the pages.
Swift
1
star
40

slush-react-app

Scaffold your next React app with webpack and webpack dev server.
JavaScript
1
star
41

generator-vanilla-webapp

This Yeoman generator helps you scaffold your next web application with Vanilla JavaScript and Webpack.
JavaScript
1
star
42

genetic-algorithm

Implement Genetic Algorithm in JavaScript
JavaScript
1
star
43

neural-flappy-bird

A tensorflow reinforcement trained flappy bird
TypeScript
1
star
44

vanilla-javascript-drag-n-drop

Created with CodeSandbox
JavaScript
1
star
45

tensorflow-playground

Tensorflow Playground
JavaScript
1
star
46

generator-vanilla-react-webapp

Vanilla React Web Application Yeoman Generator
JavaScript
1
star
47

nyc-demo

A demo of using nyc
JavaScript
1
star
48

color-magnifier

A Chrome extension for picking color on web page
JavaScript
1
star