Code Monkey home page Code Monkey logo

abexpandableview's Introduction

ABExpandableView

CI Status Version License Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first. Then run ABExpandableView.xcworkspace which is under /Example folder.

Requirements

  • Swift 4
  • Xcode 9

Installation

ABExpandableView is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'ABExpandableView'

Usage

First, import ABExpandableView to your project.

You should have 2 kinds of model objects to use this view that one of them should be section and the other one should be row.

Consider, Section and Row classes are your objects.

class Section: SectionItem {
    var identifier: String!
    var name: String!
    
    var expanded: Bool = true
    
    var rows: [RowItem] = [RowItem]()
    var rawRows: [RowItem] = [RowItem]() {
        didSet {
            rows = rawRows
        }
    }
    var selectedRows: [RowItem] = [RowItem]()
}

class Row: RowItem {
    var identifier: String!
    var name: String!
}

class MockDataProvider {
    
    class func createMockData() -> [SectionItem] {
        var array = [SectionItem]()
        
        let izmir = City()
        izmir.identifier = "35"
        izmir.name = "İzmir"
        let bornova = Town(identifier: "1", name: "Bornova")
        let urla = Town(identifier: "2", name: "Urla")
        let konak = Town(identifier: "3", name: "Konak")
        let izmirRawRows = [bornova, urla, konak]
        izmir.rawRows = izmirRawRows
        array.append(izmir)
        
        let istanbul = City()
        istanbul.identifier = "34"
        istanbul.name = "İstanbul"
        let kadikoy = Town(identifier: "4", name: "Kadıköy")
        let maltepe = Town(identifier: "5", name: "Maltepe")
        let beykoz = Town(identifier: "6", name: "Beykoz")
        let istanbulRawRows = [kadikoy, maltepe, beykoz]
        istanbul.rawRows = istanbulRawRows
        array.append(istanbul)
        
        return array
    }
    
}

After you create your models, you should open ABExpandableView with injecting those model array.

@IBAction func buttonTapped(_ sender: Any) {
        let cities = MockDataProvider.createMockData()
        let expandableSectionsViewModel = ExpandableSectionsViewModel(cities)
        let expandableSectionViewController = ExpandableSectionsViewController.newInstance(expandableSectionsViewModel)
        expandableSectionViewController.title = "Choose Town(s)"
        expandableSectionViewController.delegate = self
        self.navigationController?.pushViewController(expandableSectionViewController, animated: true)
    }

Here, let ABExpandableView handle the rest.

One last thing; You can get selected items using the delegation;

    func didSelectItems(_ items: [RowItem]) {
        let names = items.flatMap { $0.name }.joined(separator: ", ")
        // "Bornova, Kadıköy"
    }

Author

alicanbatur, [email protected]

License

ABExpandableView is available under the MIT license. See the LICENSE file for more info.

abexpandableview's People

Contributors

alicanbatur avatar emreozdil avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.