GithubHelp home page GithubHelp logo

jhaccordion's Introduction

JHAccordion

Created helper for managing opend and closed sections in an accodion style table.

Features

  • Handles click event for section header views
  • Manages opened and closed sections
  • Configure to only allow one section open at a time or multiple
  • Optional delegate to listen when sections open and close (so you can do cool animation thingies)

Installation

Drop-in Classes

Clone the repository and drop in the .h and .m files from the "Classes" directory into your project.

CocoaPods

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

pod 'JHAccordion', :git => '[email protected]:joshdholtz/JHAccordion.git'

Examples

To see full example click here: ViewController.m

Open/Close/Toggle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Initializes JHAccodion with table and sets delegate
    _accordion = [[JHAccordion alloc] initWithTableView:_tblAccordion];
    [_accordion setAllowOnlyOneOpenSection:YES];
    [_accordion setDelegate:self];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    // Just setting up the section header view
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.frame), 45.0f)];
    [view setBackgroundColor:[UIColor redColor]];
    
    // Just setting up the button to open/close a section
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5.0f, 5.0f, 80.0f, 30.f)];
    [button setBackgroundColor:[UIColor lightGrayColor]];
    [button.titleLabel setTextColor:[UIColor blackColor]];
    [button.titleLabel setFont:[UIFont systemFontOfSize:14.0f]];
    [button setTitle:@"A Button" forState:UIControlStateNormal];
    [view addSubview:button];
    
    // Tells the button to send action to JHAccordion to handle opening/closing of sections of table
    // Note: the tag of the button must be set to the section number
    [button setTag:section];
    [button addTarget:_accordion action:@selector(onClickSection:) forControlEvents:UIControlEventTouchUpInside];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 45.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // This sets all rows in the closed sections to a height of 0 (so they won't be shown)
    // and the opened section to a height of 44.0
    return ( [_accordion isSectionOpened:indexPath.section] ? 44.0f : 0.0f);
}

#pragma mark - JHAccordionDelegate

- (void)accordionOpenedSection:(NSInteger)section {
    NSLog(@"Opened section - %d", section);
}

- (void)accordionClosedSection:(NSInteger)section {
    NSLog(@"Closed section - %d", section);
}

Author

Josh Holtz, [email protected], @joshdholtz

License

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

jhaccordion's People

Contributors

brennanmke avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

spothopperllc

jhaccordion's Issues

Add support for opening/closing multiple sections at once

Currently only 1 section can be opened and closed at a time. Opening or closing another section immediately after risks crashing the app due to the way table views do not like changes while it is running updates.

I have tweaks on this forked branch. It supports several additional features but does not support opening/closing multiple sections at a time.

One reason to do this is for one section acting as a filter. Once one of the rows is selected it will close that section, change the label on the section header and open another section, which may be newly added due to the selection. The selection of that row may also have 2 or more other sections open. Previously only 1 section would open or close at a time because it was controlled by a tap from a user. This interaction is all done in response to a trigger.

https://github.com/brennanMKE/JHAccordion/tree/mytweaks

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.