Skip to content

Instantly share code, notes, and snippets.

@advantis
Created June 10, 2012 10:06
Show Gist options
  • Save advantis/2904764 to your computer and use it in GitHub Desktop.
Save advantis/2904764 to your computer and use it in GitHub Desktop.
Backport of the cell-to-nib registration API to iOS < 5.0
//
// Copyright © 2012 Yuri Kotov
//
#import <UIKit/UIKit.h>
@interface ADVTableView : UITableView
- (void) registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier;
@end
//
// Copyright © 2012 Yuri Kotov
//
#import "ADVTableView.h"
@interface ADVTableView ()
@property (nonatomic, strong) NSMutableDictionary *mapping;
@end
@implementation ADVTableView
@synthesize mapping = _mapping;
#pragma mark - ADVTableView
- (NSMutableDictionary *) mapping
{
if (!_mapping)
{
_mapping = [[NSMutableDictionary alloc] initWithCapacity:1];
}
return _mapping;
}
- (void) registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
{
if ([[self superclass] instancesRespondToSelector:_cmd])
{
[super registerNib:nib forCellReuseIdentifier:identifier];
}
else
{
NSParameterAssert(nib != nil);
NSParameterAssert(identifier != nil);
[self.mapping setObject:nib forKey:identifier];
}
}
#pragma mark - UITableView
- (id) dequeueReusableCellWithIdentifier:(NSString *)identifier
{
id cell = [super dequeueReusableCellWithIdentifier:identifier];
if (!cell && _mapping)
{
UINib *nib = [_mapping objectForKey:identifier];
cell = [[nib instantiateWithOwner:nil options:nil] lastObject];
}
return cell;
}
#pragma mark - NSObject
#if !__has_feature(objc_arc)
- (void)dealloc
{
[_mapping release];
[super dealloc];
}
#endif
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment