Giter VIP home page Giter VIP logo

collapsibletablesectionviewcontroller's Introduction

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.

collapsibletablesectionviewcontroller's People

Contributors

ezefranca avatar jeantimex avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

collapsibletablesectionviewcontroller's Issues

Custom section header view

At the moment, the section header looks like this:

screen shot 2017-08-04 at 8 43 47 am

it will be really nice if we can allow user to specify their own view/design.

I guess what we can do is to bubble up the func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? protocol method to the user, and then wrap the UIView that is provided by the user and hook up the tag gesture which triggers the collapse action. And that will make this pod even better.

Custom header view

At the moment, the section header looks like this:

screen shot 2017-08-04 at 8 43 47 am

it will be really nice if we can allow user to specify their own view/design.

I guess what we can do is to bubble up the func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? protocol method to the user, and then wrap the UIView that is provided by the user and hook up the tag gesture which triggers the collapse action. And that will make this pod even better.

Indicator Bug

I collapsed first section then collapsed second, it happened!
2017-08-07 5 29 45

so does third!

Tableview scroll down dimension error

img_5969duz
img_5969duz1


Tableview in section does not prevent the scroll position of the animation in dimension change to go to the negative values.
If the tableview data exceeds the screen, the screen slides from top to bottom when the bottom section is opened and closed.

Tap on section does nothing at all

Hi, I have successfully display the sections and child cells correctly based on your example. But when I tap on the section header the cell is not expand or collapse, I follow all the guide on the example but I still failed, any idea what I miss out or what? Thanks

Can't install pod

Hi,

When I pod install I get the message '[!] Unable to find a specification for CollapsibleTableSectionViewController'

Thanks

How do i reload the whole table?

I have the content for the CollapsibleTableSectionViewController coming from an API and i have tried using several methods like the following:

DispatchQueue.main.async{
       self.tableView.reloadData()
       self.tableView.reloadSectionIndexTitles()
       self.tableView.reloadSections(NSIndexSet(index: 1) as IndexSet, with: .automatic)
}

None of these methods calls the CollapsibleTableSectionDelegate methods to reload everything so how can i reload everything?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.