BuySellAds.com

My book Barcodes with iOS is nearing completion. Buy it now to get early access!
Our DNA is written in Objective-C
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

%d bloggers like this: