1

Hi I have a table with information from a json with addresses, I manage to sort them by neighborhood when pressing a button, I would like to create expandable & collapsable sections with the neighborhoods with the same button.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] init];
}
NSString *cellName = [[myArray objectAtIndex:indexPath.row] valueForKeyPath:@"name"];
NSString *cellState = [[myArray objectAtIndex:indexPath.row] valueForKeyPath:@"desc"];
NSString *cellTodo = [NSString stringWithFormat:@"%@: %@", cellState, cellName];
[[cell textLabel] setFont:[UIFont systemFontOfSize: 11.0]];
cell.textLabel.text = cellTodo;
return cell;
}


- (IBAction)PorBarrio:(id)sender {
NSSortDescriptor * sortDesc = [[NSSortDescriptor alloc] initWithKey:@"desc" ascending:YES];
[myArray sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
[myTableView reloadData];
}
dastjuso
  • 161
  • 2
  • 10
  • Check Apple sample code [Table View Animations and Gestures](http://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html) for collapsible table view – Valent Richie Jul 03 '13 at 23:41
  • check http://stackoverflow.com/questions/15442697/how-to-create-an-accordion-with-uitableview-under-a-uitableview – saturngod Jul 04 '13 at 01:47
  • I just started a [GitHub project](https://github.com/i2097i/SRExpandableTableView) for an iOS expandable UITableView called SRExpandableTableView. I hope it helps! – i2097i Aug 11 '13 at 07:46

2 Answers2

3

My personal vote is for SLExpandableTableView. You can find it here on GitHub.

It's brilliantly easy to use but not well documented; currently there are no examples. It basically acts as an extension on the regular UITableView class, and works great with storyboard prototypes / custom look & feel. So you don't have to do a ton of re-work if you've already got a UITableView going.

It took me a bit of figuring out on exactly how to implement the expandingCellForSection: method - here is mine so that it makes it a bit easier for others:

- (UITableViewCell<UIExpandingTableViewCell> *)tableView:(SLExpandableTableView *)tableView expandingCellForSection:(NSInteger)section {

    NSString *CellIdentifier = @"amenitysectioncell_immediate";
    AmenitySectionTableViewCell *cell = (AmenitySectionTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[AmenitySectionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdientifier];
    }

    cell.sectionTitle.text = @"blah blah";

    return cell;
}

The trick is to subclass UITableViewCell for your sections cells:

@interface AmenitySectionTableViewCell : UITableViewCell<UIExpandingTableViewCell>  

@property (nonatomic,strong) IBOutlet UILabel *sectionTitle; 

@end

Enjoy!

capikaw
  • 12,232
  • 2
  • 43
  • 46
  • 1
    thank you @capikaw. please share your basic working example! i continue to crash :(. there still doesn't seem to be a buildable example posted on github. – tarabyte Feb 19 '14 at 23:50
  • It doesn't work if you have a UIViewController (and a UITableView inside) instead of UITableViewController – Chanchal Raj Dec 07 '15 at 12:35
  • It certainly does work. I did it with UIViewController following [this forum](https://github.com/OliverLetterer/SLExpandableTableView/issues/14) – Taku Jun 04 '16 at 06:16
0
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 

*Use uiTableView delegate method "viewForHeaderInSection" and return a custom UIView.

*Add a UIButton as subview with action "expandable:(id)sender" check the sender id as section number and reload the table view.

Check this Link :

http://iostechnotips.blogspot.in/2014/05/expandable-uitableview.html

example :

 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection(NSInteger)section {
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0,2 , tableView.frame.size.width, 20)];

    UIButton * headerButton=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
    [headerButton addTarget:self action:@selector(expandeble:) forControlEvents:UIControlEventTouchDown];
    [headerButton setTag:section];
    [headerButton setTitle:[NSString stringWithFormat:@"Section %d",section] forState:UIControlStateNormal] ;
    [view addSubview:headerButton];
    view.backgroundColor=[UIColor grayColor];
    return view;
}

-(IBAction)expandeble:(id)sender{
    UIButton* myButton = (UIButton*)sender;
    [self exapand:(int)myButton.tag];
}

-(void)exapand:(int)tag{
    if (senctionIndex != tag) {
    senctionIndex=tag;
    [_expandableTable reloadData];
}}
iPeter
  • 1,330
  • 10
  • 26
Rahul
  • 79
  • 1
  • 5