Skip to main content

Expanding/Collapsing TableView Sections - iOS

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.

Comments

Popular Posts

React Native - Text Input

In this chapter, we will show you how to work with  TextInput  elements in React Native. The Home component will import and render inputs. App.js import React from 'react' ; import Inputs from './inputs.js' const App = () => { return ( < Inputs /> ) } export default App Inputs We will define the initial state. After defining the initial state, we will create the  handleEmail  and the  handlePassword  functions. These functions are used for updating state. The  login()  function will just alert the current value of the state. We will also add some other properties to text inputs to disable auto capitalisation, remove the bottom border on Android devices and set a placeholder. inputs.js import React , { Component } from 'react' import { View , Text , TouchableOpacity , TextInput , StyleSheet } from 'react-native' class Inputs extends Component { state = { ...

An introduction to Size Classes for Xcode 8

Introduction to Size Classes for Xcode In iOS 8, Apple introduced  size classes , a way to describe any device in any orientation. Size classes rely heavily on auto layout. Until iOS 8, you could escape auto layout. IN iOS8, Apple changed several UIKit classes to depend on size classes. Modal views, popovers, split views, and image assets directly use size classes to determine how to display an image. Identical code to present a popover on an iPad  causes a iPhone to present a modal view. Different Size Classes There are two sizes for size classes:  compact , and  regular . Sometime you’ll hear about any.  Any  is the generic size that works with anything. The default Xcode layout, is  width:any height:any . This layout is for all cases. The Horizontal and vertical dimensions are called  traits , and can be accessed in code from an instance of  UITraitCollection . The  compact  size descr...

What are the Alternatives of device UDID in iOS? - iOS7 / iOS 6 / iOS 5 – Get Device Unique Identifier UDID

Get Device Unique Identifier UDID Following code will help you to get the unique-device-identifier known as UDID. No matter what iOS user is using, you can get the UDID of the current iOS device by following code. - ( NSString *)UDID { NSString *uuidString = nil ; // get os version NSUInteger currentOSVersion = [[[[[UIDevice currentDevice ] systemVersion ] componentsSeparatedByString: @" . " ] objectAtIndex: 0 ] integerValue ]; if (currentOSVersion <= 5 ) { if ([[ NSUserDefaults standardUserDefaults ] valueForKey: @" udid " ]) { uuidString = [[ NSUserDefaults standardDefaults ] valueForKey: @" udid " ]; } else { CFUUIDRef uuidRef = CFUUIDCreate ( kCFAllocatorDefault ); uuidString = ( NSString *) CFBridgingRelease ( CFUUIDCreateString ( NULL ,uuidRef)); CFRelease (uuidRef); [[ NSUserDefaults standardUserDefaults ] setObject: uuidString ForKey: @" udid " ]; [[ NSUserDefaults standardUserDefaults ] synchro...