Skip to content

Reusable AccordianView in swift. It will take less than 2 min to integrate it with your app.

License

Notifications You must be signed in to change notification settings

milankamilya/AccordianView

Repository files navigation

AccordianView

Reusable AccordianView in swift. It will take less than 2 min to integrate it with your app.

Click on this GIF to have a full look on how the pull project looks like

YouTube LinkYouTube Link

Installation

You need do it manually. Download and add the following file to your project.

MKAccordionView.swift

Usage

MKAccordionView follows delegation patterns like UITableView. So, if you have worked with UITableView, it is very easy for you to implement it. As you need to implement few delegate and datasource method of MKAccordionView.

// MARK: - Implemention of MKAccordionViewDelegate method
extension ViewController : MKAccordionViewDelegate {
    
    func accordionView(accordionView: MKAccordionView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 50
    }
    
    func accordionView(accordionView: MKAccordionView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    
    func accordionView(accordionView: MKAccordionView, viewForHeaderInSection section: Int, isSectionOpen sectionOpen: Bool) -> UIView? {
     
        var view : UIView! = UIView(frame: CGRectMake(0, 0, CGRectGetWidth(accordionView.bounds), 50))

        // Customize section header view according to your need
        // "sectionOpen" will let you know section header is expended or collapsed. And you can change the 
        //  background color / image accordingly
        
        return view
        
    }
}

As we need to implement UITableviewDatasource in case of UITableView, here we need to do the same.

// MARK: - Implemention of MKAccordionViewDatasource method
extension ViewController : MKAccordionViewDatasource {
    
    func numberOfSectionsInAccordionView(accordionView: MKAccordionView) -> Int {
        return <# No of Sections #>
    }
    
    func accordionView(accordionView: MKAccordionView, numberOfRowsInSection section: Int) -> Int {
        return <# No of rows of a specific section #>
    }
    
    func accordionView(accordionView: MKAccordionView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell : UITableViewCell? = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: nil)
        
        // Customize cell according to your use
        
        return cell!
    }
}

When you are going to initialize the MKAccordionView, don’t forget to set delegate and datasource to self. Next add the object to view.

var accordionView : MKAccordionView = MKAccordionView(frame: CGRectMake(0, 22, CGRectGetWidth(view.bounds), CGRectGetHeight(view.bounds)))
accordionView.delegate = self
accordionView.dataSource = self
view.addSubview(accordionView)

Find it difficult to implement?

License

AccordianView is under MIT license so feel free to use it!

Author

Created by Milan Kamilya. Please feel free to drop an email if you have any question. [email protected] or tweet me @Milan_Kamilya

About

Reusable AccordianView in swift. It will take less than 2 min to integrate it with your app.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages