Skip to content

Instantly share code, notes, and snippets.

@appleios
Created September 21, 2015 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appleios/3953ef65d831a5a7cf89 to your computer and use it in GitHub Desktop.
Save appleios/3953ef65d831a5a7cf89 to your computer and use it in GitHub Desktop.
XibLoader
//
// VIOSXibLoader.h
// Created by Aziz Latypov on 8/21/15.
//
// If your TableViewCells are in Xib files and you name them in some convencion
// described with regular expression, you can load them into a table like this:
// In your viewDidLoad method call
// [[VIOSXibLoader defaultLoader] loadXibsForTableView:self.tableView
// withPattern:@"^MyModelClassName(\\w*)TableViewCell$"];
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface VIOSXibLoader : NSObject
+ (instancetype)defaultLoader;
- (void)loadXibsForTableView:(UITableView*)tableView withPattern:(NSString*)pattern;
@end
//
// VIOSXibLoader.m
// Created by Aziz Latypov on 8/21/15.
//
#import "VIOSXibLoader.h"
@implementation VIOSXibLoader
+ (instancetype)defaultLoader
{
static id _sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}
- (void)loadXibsForTableView:(UITableView*)tableView withPattern:(NSString*)pattern
{
NSBundle *bundle = [NSBundle mainBundle];
NSRegularExpression *regularExpression =
[[NSRegularExpression alloc] initWithPattern:pattern
options:NSRegularExpressionCaseInsensitive
error:nil];
NSArray *paths = [bundle pathsForResourcesOfType:@"nib" inDirectory:nil];
[paths enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *path = (NSString*)obj;
NSString *name = [[path lastPathComponent] stringByDeletingPathExtension];
NSArray *matches = [regularExpression matchesInString:name options:0 range:NSMakeRange(0, name.length)];
if ([matches count]) {
UINib *cellNib = [UINib nibWithNibName:name bundle:nil];
if (cellNib) {
[tableView registerNib:cellNib forCellReuseIdentifier:name];
}else{
NSLog(@"Error: Can not load xib with name %@",name);
}
}
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment