Skip to content

Instantly share code, notes, and snippets.

@hfm
Created October 29, 2012 02:56
Show Gist options
  • Save hfm/3971223 to your computer and use it in GitHub Desktop.
Save hfm/3971223 to your computer and use it in GitHub Desktop.
Snipped Customised UITableViewCell
// Inspired by http://stackoverflow.com/questions/540345/how-do-you-load-custom-uitableviewcells-from-xib-files
#import <UIKit/UIKit.h>
@interface BaseCell : UITableViewCell
/* Load Cell-Object from Nib-Name */
+ (BaseCell*)cellFromNibNamed:(NSString *)nibName;
@end
#import "BaseCell.h"
/* declaration of private property */
@interface BaseCell()
@property (weak, nonatomic) IBOutlet HOGEHOGE *hogehoge;
@end
@implementation BaseCell
#pragma mark - Life Cycle
+ (BaseCell *)cellFromNibNamed:(NSString *)nibName
{
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:NULL];
NSEnumerator *nibEnumerator = [nibContents objectEnumerator];
BaseCell *customCell = nil;
NSObject* nibItem = nil;
while ((nibItem = [nibEnumerator nextObject]) != nil)
{
if ([nibItem isKindOfClass:[BaseCell class]])
{
customCell = (BaseCell *)nibItem;
break; // we have a winner
}
}
return customCell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
NSLog(@"%s",__func__);
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
#import <UIKit/UIKit.h>
@class BaseCell;
@interface BaseTableView : UITableView
@end
#import "BaseCell.h"
#import "BaseTableView.h"
@implementation BaseTableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BaseCell *_basecell = [tableView dequeueReusableCellWithIdentifier:@"orgCell"];
if (_basecell == nil)
{
_basecell = (BaseCell *)[BaseCell cellFromNibNamed:@"Cell"];
}
return _basecell;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment