Ad

Our DNA is written in Swift
Jump

Expanding/Collapsing TableView Sections

While giving many designers a headache the Twitter app still serves as template on how to solve a variety of UX riddles. One of which is the situation where one might want to have sections in a tableview that possess the ability to expand from one row to several and collapse vice versa.

The eye of the experienced developer sees two challenges contained therein: 1) grafting a mechanism for collapsing and expanding onto UITableView in a reusable way 2) making custom accessory views that look like a rotated version of the disclosure indicator, pointing upwards or downwards and also changing color when highlighted.

In this article I present my solution to this UX riddle. At the same time I will demonstrate how NSMutableIndexSet can be used to our advantage. In contrast to the pull-to-reload method previously discussed, this does not contain anything remotely patentable.

Update March 12th, 2013: Cleaned up version of the custom-colored accessory is now available via DTFoundation, the example project is now part of our Examples collection on GitHub. Please note that if you use this code you have to attribute it to us or buy a Non-Attribution License.

I could not figure out a way to get this functionality as class extension, which would have been nice. But this is not possible if you need an instance variable to keep track of something. The finished subclass will look and behave just like the original Twitter app for iPhone does.

We start out by creating a new UITableView subclass which has a NSMutableSet instance variable to contain the section numbers which are already expanded. Don’t let yourself be daunted by the sheer amount of code. That’s really much ado about nothing.

UITableViewControllerWithExpandoSections.h

@interface UITableViewControllerWithExpandoSections : UITableViewController 
{
    NSMutableIndexSet *expandedSections;
}

NSIndexSet and the mutable cousin NSMutableIndexSet allow you to store an index, i.e. a number. It has methods to add such a number, remove it and query if it is contained in it. Being a set means that it is not ordered and each entry is automatically unique.

Here’s the code, please go through it and see if you can figure out what’s happening.

UITableViewControllerWithExpandoSections.m

#import "UITableViewControllerWithExpandoSections.h"
#import "DTCustomColoredAccessory.h"
 
@implementation UITableViewControllerWithExpandoSections
 
- (void)dealloc
{
    [expandedSections release];
    [super dealloc];
}
 
- (void)viewDidLoad
{
    [super viewDidLoad];
 
    if (!expandedSections)
    {
        expandedSections = [[NSMutableIndexSet alloc] init];
    }
}
 
- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section
{
    if (section>0) return YES;
 
    return NO;
}
 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 3;
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([self tableView:tableView canCollapseSection:section])
    {
        if ([expandedSections containsIndex:section])
        {
            return 5; // return rows when expanded
        }
 
        return 1; // only top row showing
    }
 
    // Return the number of rows in the section.
    return 1;
}
 
- (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] autorelease];
    }
 
    // Configure the cell...
 
    if ([self tableView:tableView canCollapseSection:indexPath.section])
    {
        if (!indexPath.row)
        {
            // first row
            cell.textLabel.text = @"Expandable"; // only top row showing
 
            if ([expandedSections containsIndex:indexPath.section])
            {
                cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
            }
            else
            {
                cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
            }
        }
        else
        {
            // all other rows
            cell.textLabel.text = @"Some Detail";
            cell.accessoryView = nil;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    else
    {
        cell.accessoryView = nil;
        cell.textLabel.text = @"Normal Cell";
 
    }
 
    return cell;
}

This code introduces method tableView:canCollapseSection: that can make individual sections collapsible or not. In this example I make sections 1 and 2 such, section 0 cannot expand.

The mutable index set gets instantiated in viewDidLoad and released in dealloc. So far so good. Knowing what I told you above about index set you can easily see how this is used to determine whether a section should show as expanded or collapsed. If the index is in the set, then numberOfRowsInSection returns the full number of detail cells, otherwise 1 for the header. You can also see that I’m using a DTCustomColoredAccessory, more on that later.

The expansion and collapse animation is simply achieved by using the built-in tableview animations to insert and delete cells. Note that these are only animating, if you don’t make sure that numberOfRowsInSection returns the correct new number BEFORE invoking the animating method then you will get an exception. So: first make sure that the number is changed, then call the animation.

Here’s the code for that.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self tableView:tableView canCollapseSection:indexPath.section])
    {
        if (!indexPath.row)
        {
            // only first row toggles exapand/collapse
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
 
            NSInteger section = indexPath.section;
            BOOL currentlyExpanded = [expandedSections containsIndex:section];
            NSInteger rows;
 
            NSMutableArray *tmpArray = [NSMutableArray array];
 
            if (currentlyExpanded)
            {
                rows = [self tableView:tableView numberOfRowsInSection:section];
                [expandedSections removeIndex:section];
 
            }
            else
            {
                [expandedSections addIndex:section];
                rows = [self tableView:tableView numberOfRowsInSection:section];
            }
 
            for (int i=1; i<rows; i++)
            {
                NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i 
                                                               inSection:section];
                [tmpArray addObject:tmpIndexPath];
            }
 
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
 
            if (currentlyExpanded)
            {
                [tableView deleteRowsAtIndexPaths:tmpArray 
                                 withRowAnimation:UITableViewRowAnimationTop];
 
                cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
 
            }
            else
            {
                [tableView insertRowsAtIndexPaths:tmpArray 
                                 withRowAnimation:UITableViewRowAnimationTop];
                cell.accessoryView =  [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
 
            }
        }
    }
}

In both cases I have to construct an array that contains the index paths of the rows 1 through 4, once for inserting, once for deleting. At the same time I’m grabbing the header cell so that I can update the accessory view with the appropriate arrow direction. Now, let me also give you the custom accessory view, which is based on my previous article on how to custom-draw a disclosure indicator I simply added a type enum and modifications in drawRect.

DTCustomColoredAccessory.h

typedef enum 
{
    DTCustomColoredAccessoryTypeRight = 0,
    DTCustomColoredAccessoryTypeUp,
    DTCustomColoredAccessoryTypeDown
} DTCustomColoredAccessoryType;
 
@interface DTCustomColoredAccessory : UIControl
{
	UIColor *_accessoryColor;
	UIColor *_highlightedColor;
 
    DTCustomColoredAccessoryType _type;
}
 
@property (nonatomic, retain) UIColor *accessoryColor;
@property (nonatomic, retain) UIColor *highlightedColor;
 
@property (nonatomic, assign)  DTCustomColoredAccessoryType type;
 
+ (DTCustomColoredAccessory *)accessoryWithColor:(UIColor *)color type:(DTCustomColoredAccessoryType)type;
 
@end

DTCustomColoredAccessory.m

#import "DTCustomColoredAccessory.h"
 
@implementation DTCustomColoredAccessory
 
- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
		self.backgroundColor = [UIColor clearColor];
    }
    return self;
}
 
- (void)dealloc
{
	[_accessoryColor release];
	[_highlightedColor release];
    [super dealloc];
}
 
+ (DTCustomColoredAccessory *)accessoryWithColor:(UIColor *)color type:(DTCustomColoredAccessoryType)type
{
	DTCustomColoredAccessory *ret = [[[DTCustomColoredAccessory alloc] initWithFrame:CGRectMake(0, 0, 15.0, 15.0)] autorelease];
	ret.accessoryColor = color;
    ret.type = type;
 
	return ret;
}
 
- (void)drawRect:(CGRect)rect
{
    CGContextRef ctxt = UIGraphicsGetCurrentContext();
 
    const CGFloat R = 4.5;
 
    switch (_type) 
    {
        case DTCustomColoredAccessoryTypeRight:
        {
            // (x,y) is the tip of the arrow
            CGFloat x = CGRectGetMaxX(self.bounds)-3.0;;
            CGFloat y = CGRectGetMidY(self.bounds);
 
            CGContextMoveToPoint(ctxt, x-R, y-R);
            CGContextAddLineToPoint(ctxt, x, y);
            CGContextAddLineToPoint(ctxt, x-R, y+R);
 
            break;
        }    
 
        case DTCustomColoredAccessoryTypeUp:
        {
            // (x,y) is the tip of the arrow
            CGFloat x = CGRectGetMaxX(self.bounds)-7.0;;
            CGFloat y = CGRectGetMinY(self.bounds)+5.0;
 
            CGContextMoveToPoint(ctxt, x-R, y+R);
            CGContextAddLineToPoint(ctxt, x, y);
            CGContextAddLineToPoint(ctxt, x+R, y+R);
 
            break;
        } 
 
        case DTCustomColoredAccessoryTypeDown:
        {
            // (x,y) is the tip of the arrow
            CGFloat x = CGRectGetMaxX(self.bounds)-7.0;;
            CGFloat y = CGRectGetMaxY(self.bounds)-5.0;
 
            CGContextMoveToPoint(ctxt, x-R, y-R);
            CGContextAddLineToPoint(ctxt, x, y);
            CGContextAddLineToPoint(ctxt, x+R, y-R);
 
            break;
        } 
 
        default:
            break;
    }
 
    CGContextSetLineCap(ctxt, kCGLineCapSquare);
    CGContextSetLineJoin(ctxt, kCGLineJoinMiter);
    CGContextSetLineWidth(ctxt, 3);
 
	if (self.highlighted)
	{
		[self.highlightedColor setStroke];
	}
	else
	{
		[self.accessoryColor setStroke];
	}
 
	CGContextStrokePath(ctxt);
}
 
- (void)setHighlighted:(BOOL)highlighted
{
	[super setHighlighted:highlighted];
 
	[self setNeedsDisplay];
}
 
- (UIColor *)accessoryColor
{
	if (!_accessoryColor)
	{
		return [UIColor blackColor];
	}
 
	return _accessoryColor;
}
 
- (UIColor *)highlightedColor
{
	if (!_highlightedColor)
	{
		return [UIColor whiteColor];
	}
 
	return _highlightedColor;
}
 
@synthesize accessoryColor = _accessoryColor;
@synthesize highlightedColor = _highlightedColor;
@synthesize type = _type;
 
@end

This is technically a quite simple exercise, but still a ton of code is required to achieve the desired effect. What have we learned? NSIndexSet is a comfortable way to store indexes. We get a cool animation by using the insertion and deletion methods of UITableView, provided we change the number of rows method’s result in advance of invoking the animation methods. And finally we can go the extra mile and create a custom accessory view that also reacts appropriately to being highlighted.


Categories: Recipes

73 Comments »

  1. There is a really good video from WWDC2010 – Session 128 – Mastering Table Views
    I’m sure the sample code is also available.

  2. Hi Oliver

    Good timing – I was going to build something like this today myself.

    One question, I noticed clicking on your custom accessory button does nothing. It would be nice if it expanded/contracted the table, the same as tapping on the cell itself does. I had a quick look but couldn’t find an easy solution. Calls like accessoryButtonTappedForRowWithIndexPath don’t get invoked for custom views. I can add a target/action to the accessory view, but it doesn’t know what row its on (its super is the table cell, but this also doesn’t know what row its on either).
    What are your thoughts?

    Cheers
    Rob

  3. Also worth looking at Apple’s “Table View Animations And Gestures” sample code (ADC account required – http://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html)

  4. this is fantastic…..just what i was looking for.
    but is there any way we can get a sample code?
    Please!

  5. all the code is in the article.

  6. See my first message. Maybe you’ll find more code there.

  7. Thanks for this. I really appreciate it! πŸ™‚

    BTW, I noticed tableView is not used in this method:

    – (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section
    {
    if (section>0) return YES;

    return NO;
    }

    Is there a need to pass the tableView?

    Thanks.

    Andrew

  8. There is no need, but generally delegate methods should contain the name of who is calling the delegate so that you know where this method belongs to. Also it enables the developer to use the same table view controller for multiple table views because you can change the returned value based on which table it is for.

  9. Ah yes, that is very true. Thanks for the explanation.

  10. Any news/ideas on this? Would be really great to know how to fix this!
    Thanks a lot, greetings from Germany,
    Chris

  11. The custom accessory could be made into a button that would somehow invoke the accessory. to identify the row I would simply use the button’s tag to store this number.

  12. Thank you very much! Your article is very useful! Exactly what I was looking for.

  13. sounds like a good idea, i’ll give it a try – thanks!!

  14. Thank you so much for this. I was able to get the cells working on my own, but your accessory arrow code is exactly what I was looking for. I really appreciate you posting this. (Bought Linguan to show my support, and that seems awesome, too!)

  15. Hi ,

    I am new to Objective c. This is fantastic. I am also facing a similar problem that u explain in your example. In this example u r having only row per section which can be expanded but in my problem i have multiple rows in each section and all the rows can be expanded . For Ex: I have list of stores which i am showing as rows of a section , now on click of stores user should be able to see clients for that store.Store row should expand and client rows should appear below it . I am not able to achieve this problem. Kindly help me how to expand and collapse multiple rows in a section.

  16. Hello, great article.

    There’s one thing, though: I get miscounted-rows exception! When I altered the rows’ loop to start from zero, rather than one, it got fixed.

  17. [tableView reloadData]; forces the accessory in every section/row to point the arrow down, regardless of what I set in my code.

    Collapse all sections. Reload. All sections expand, even though the expandedSections array wasn’t reset. Row at index 1 in the force-expanded section with the lowest number index gets the DTCustomColoredAccessory, which, depending on the section, will either control the next section down or just plain crash the app with an uncaught exception (invalid row count).

    Any possible help? I can post some code.

  18. Bug was somewhere in my code, corrected the problem by having all sections collapsed by default. Close enough, and it’s probably better like that.

    Great code, by the way. Some small tweaks and it works fine in iOS 5.x.

  19. Hello mate!
    I would like to thank you very much for the time you have spent to create this helpful article.
    I’m a starting iOS developer and this has helped me a lot.

    I would like to ask one question pertaining to your article.
    What if I only want one row to be expanded at a time?

    I have been tinkering with the code and I can’t seem to figure out my problem.

    I would like if you tap a row, it expands as usual.
    But if you tap another; it returns the previous cell to normal, and then it expands the new cell!
    So there will be only one expanded row at a time.

    Thank you!
    -Joey

  20. Hello !
    First of all, great job ! The tutorial is clear and it works perfectly.
    As Joey, I wish I could unexpand previous sections when a new one is expanded but I can’t figure out how I could do that.
    Any help ?
    Thanks a lot and greetings from France

  21. I am newbie to iOS development…This tutorial is just awesome..I needed this …but I have one requirement..that I want to keep the first row open by default…ie the first expandable section should be open by default…I am unable to figure it out…is there any way??

  22. The archive is broken

  23. First off, awesome tutorial! This is just what I needed. πŸ™‚
    Secondly, I did a small change in the code. Since you don’t have to synthasize properties or declare ivars in iOS 6, I just used self keyword to access the properties straight away in methods like – (UIColor *)accessoryColor and – (UIColor *)highlightedColor (ex: self.accessoryColor instead of !_accessoryColor). At the time of writing, it didn’t show any warnings or anything but when I ran it, it threw a BAD_ACCESS error. Once I reverted back to using ivars the errors disappeared. Why can’t I use self to access the properties? How can I update the code to suit later versions of iOS(5 and 6 preferably).

    Thanks

  24. isuru, were you able to download and extract the zip archive at:
    http://www.speedyshare.com/VwNx9/Expanding-Collapsing-TableView-Sections.zip

  25. @kostadinnm I downloaded it and tried to unzip it in my Mac and it unzips to another file with the .cpgz extension. Unzipping that would make another zip file. The loop goes on and on. Tried unzipping it in Windows and it threw an error saying the file is corrupted.

  26. Please note that the most uptodate version of DTCustomColoredAccessory can be found in DTFoundation:
    https://github.com/Cocoanetics/DTFoundation/blob/master/Core/Source/iOS/DTCustomColoredAccessory.m

    And there is even beautiful documentation:
    https://docs.cocoanetics.com/DTFoundation/Classes/DTCustomColoredAccessory.html

  27. Hey Oliver,

    I’ve been trying to tweak this a little bit. I’m trying to add a custom UITableViewCell for the drop down cells. So I changed the cellForRowAtIndexPath a little bit. I’ve posted the code here. (http://pastebin.com/TPQzk9n0) Everything else works fine still but the custom cell does not show up. Instead there is a blank cell. Can you please take a quick look and tell me what I’m missing here?

    The custom cell works. I put it in a separate project to test it out and the custom cell appears. The custom cell has only 2 labels and a button.

  28. @Drop. Great Tutorial. However, I have not been able to compile the code. I get some weird “missing @end” errors. I tried downloading the code but I get the same error as Isuru Nanayakkara… the looping unzip. Could you please help me. BTW, I’m new to objective-c and iOS development.

  29. @jslapalme : That missing @end error occurred to me also. I don’t remember well now but I think it was because of an extra unwanted @end or something. Try adding and removing the @end keyword at the line where it throws the error.

  30. Cleaned up version of the custom-colored accessory is now available via DTFoundation, the example project is now part of our Examples collection on GitHub: https://github.com/cocoanetics/Examples

    Please note that if you use this code you have to attribute it to us or buy a Non-Attribution License.

  31. how to exapand UITableviewCell first run app

  32. Hi, Thanks a lot for the post. I have modified the code in which I can keep track and close the previous section.
    At one point of time, only one section would be opened.

    if (currentlyExpanded)
    {
    [tableView deleteRowsAtIndexPaths:tmpArray
    withRowAnimation:UITableViewRowAnimationTop];

    cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];

    }
    else
    {
    [tableView insertRowsAtIndexPaths:tmpArray
    withRowAnimation:UITableViewRowAnimationTop];

    if (self.openSectionIndex != – 1)
    {
    [tmpArray removeAllObjects];
    rows = [self tableView:tableView numberOfRowsInSection:self.openSectionIndex];
    [expandedSections removeIndex:self.openSectionIndex];

    for (int i=1; i<rows; i++)
    {
    NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
    inSection:self.openSectionIndex];
    [tmpArray addObject:tmpIndexPath];
    }
    [tableView deleteRowsAtIndexPaths:tmpArray
    withRowAnimation:UITableViewRowAnimationTop];

    }
    self.openSectionIndex = section;
    cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];

    }

  33. @taimur ajmal

    where should i use your code….
    reply plz….

  34. i getting error when i tap section 0…
    attempt to delete row 7 from section 0 which only contains 1 rows before the update

  35. This is really helpful thanks a lot. Very elegant also

  36. @Taimur Ajmal

    i make changes to contiously show the child list for each parent. Above code have only one time show the child list for same parent.

    if (currentlyExpanded)
    {
    [tableView deleteRowsAtIndexPaths:tmpArray
    withRowAnimation:UITableViewRowAnimationTop];

    cell.accessoryView = [AccessoryView accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
    openSectionIndex = -1; //Added this line

    }
    else
    {
    [tableView insertRowsAtIndexPaths:tmpArray
    withRowAnimation:UITableViewRowAnimationTop];

    if (openSectionIndex != -1)
    {
    [tmpArray removeAllObjects];
    rows = [self tableView:tableView numberOfRowsInSection:openSectionIndex];
    [expandedSections removeIndex:openSectionIndex];

    for (int i=1; i<rows; i++)
    {
    NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
    inSection:openSectionIndex];
    [tmpArray addObject:tmpIndexPath];
    }
    [tableView deleteRowsAtIndexPaths:tmpArray
    withRowAnimation:UITableViewRowAnimationTop];

    }
    openSectionIndex = section;
    cell.accessoryView = [AccessoryView accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];

    }

  37. You may take a look at this accordion example in Swift: https://github.com/tadija/AEAccordion

    It’s got very little code to create accordion effect (not by using sections but cells), and as a bonus there is also a solution to use XIB files inside other XIB files (useful for custom cells which use custom views).

  38. If i want to add row after expanding cell then how would i do ?

    Ex :-
    Normal Cell
    Expandable cell
    -some cell
    -some cell
    Normal cell 2

    How would i add normal cell 2 ?

  39. Very helpfull. Working great…

  40. How can I collapse a section automatically, when another section is expanded.

  41. If we pass the array of elements to diplay rows, it always access the first element of array. whereas the expected output should be like it should display all array items in table view.

    This is the code :
    – (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
    return [stringArray count];
    }

    – (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {
    if ([self tableView:tableView canCollapseSection:section])
    {
    if ([expandedSections containsIndex:section])
    {
    return 5; // return rows when expanded
    }
    return 1; // only top row showing

    }

    // Return the number of rows in the section.

    return 1;

    }

    Please help me out. It should display all the row from array with respective expandable rows.
    Thanks in advance

  42. Hai i need Multiple Expandable Tableview same like this in objective c