Created
September 21, 2015 10:15
-
-
Save appleios/3953ef65d831a5a7cf89 to your computer and use it in GitHub Desktop.
XibLoader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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