Giter Site home page Giter Site logo

laurentiuungur / luexpandabletableview Goto Github PK

View Code? Open in Web Editor NEW
127.0 10.0 19.0 2.26 MB

A subclass of UITableView with expandable and collapsible sections

License: MIT License

Swift 97.07% Ruby 2.93%
cocoapods collapsible-sections xcode ios tableview expandable swift cocoa pod collapsible

luexpandabletableview's Introduction

LUExpandableTableView

A subclass of UITableView with expandable and collapsible sections

Swift 5 Swift Package Manager compatible Pod Version Pod Platform Pod License

Preview

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ sudo gem install cocoapods

CocoaPods 1.14.0+ is required.

To integrate LUExpandableTableView into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '13.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'LUExpandableTableView'
end

Then, run the following command:

$ pod install

Swift Package Manager

To integrate using Apple's Swift Package Manager, add the following as a dependency to your Package.swift:

.package(url: "https://github.com/LaurentiuUngur/LUExpandableTableView", from: Version(6, 0, 0))

Manually

If you prefer not to use either of the before mentioned dependency managers, you can integrate LUExpandableTableView into your project manually.

Usage

  • Import LUExpandableTableView into your project.
import LUExpandableTableView
  • Register a cell for an instance of LUExpandableTableView. Registered class must be a subclass of UITableViewCell. This step is not be necessary if you use storyboard.
expandableTableView.register(MyTableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
  • Register a header for an instance of LUExpandableTableView. Registered class must be a subclass of LUExpandableTableViewSectionHeader Keep in mind that you cannot use storyboard in order to do this.
expandableTableView.register(UINib(nibName: "MyExpandableTableViewSectionHeader", bundle: Bundle.main), forHeaderFooterViewReuseIdentifier: sectionHeaderReuseIdentifier)
  • Set as data source and delegate.
expandableTableView.expandableTableViewDataSource = self
expandableTableView.expandableTableViewDelegate = self
  • Implement LUExpandableTableViewDataSource and LUExpandableTableViewDelegate protocols.
// MARK: - LUExpandableTableViewDataSource

extension ViewController: LUExpandableTableViewDataSource {
   func numberOfSections(in expandableTableView: LUExpandableTableView) -> Int {
       return 42
   }
   
   func expandableTableView(_ expandableTableView: LUExpandableTableView, numberOfRowsInSection section: Int) -> Int {
       return 3
   }
   
   func expandableTableView(_ expandableTableView: LUExpandableTableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       guard let cell = expandableTableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as? MyTableViewCell else {
           assertionFailure("Cell shouldn't be nil")
           return UITableViewCell()
       }
       
       cell.label.text = "Cell at row \(indexPath.row) section \(indexPath.section)"
       
       return cell
   }
   
   func expandableTableView(_ expandableTableView: LUExpandableTableView, sectionHeaderOfSection section: Int) -> LUExpandableTableViewSectionHeader {
       guard let sectionHeader = expandableTableView.dequeueReusableHeaderFooterView(withIdentifier: sectionHeaderReuseIdentifier) as? MyExpandableTableViewSectionHeader else {
           assertionFailure("Section header shouldn't be nil")
           return LUExpandableTableViewSectionHeader()
       }
       
       sectionHeader.label.text = "Section \(section)"
       
       return sectionHeader
   }
}

// MARK: - LUExpandableTableViewDelegate

extension ViewController: LUExpandableTableViewDelegate {
   func expandableTableView(_ expandableTableView: LUExpandableTableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
       /// Returning `UITableViewAutomaticDimension` value on iOS 9 will cause reloading all cells due to an iOS 9 bug with automatic dimensions
       return 50
   }
   
   func expandableTableView(_ expandableTableView: LUExpandableTableView, heightForHeaderInSection section: Int) -> CGFloat {
       /// Returning `UITableViewAutomaticDimension` value on iOS 9 will cause reloading all cells due to an iOS 9 bug with automatic dimensions
       return 69
   }
   
   // MARK: - Optional
   
   func expandableTableView(_ expandableTableView: LUExpandableTableView, didSelectRowAt indexPath: IndexPath) {
       print("Did select cell at section \(indexPath.section) row \(indexPath.row)")
   }
   
   func expandableTableView(_ expandableTableView: LUExpandableTableView, didSelectSectionHeader sectionHeader: LUExpandableTableViewSectionHeader, atSection section: Int) {
       print("Did select cection header at section \(section)")
   }
   
   func expandableTableView(_ expandableTableView: LUExpandableTableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
       print("Will display cell at section \(indexPath.section) row \(indexPath.row)")
   }
   
   func expandableTableView(_ expandableTableView: LUExpandableTableView, willDisplaySectionHeader sectionHeader: LUExpandableTableViewSectionHeader, forSection section: Int) {
       print("Will display section header for section \(section)")
   }
}

For more usage details please see example app

Requirements

  • Xcode 15.0+
  • Swift 5.9+
  • iOS 13.0+

Author

License

  • LUExpandableTableView is available under the MIT license.

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.