Skip to content

Instantly share code, notes, and snippets.

@MonsieurDart
Created March 24, 2011 11:00
Show Gist options
  • Save MonsieurDart/884886 to your computer and use it in GitHub Desktop.
Save MonsieurDart/884886 to your computer and use it in GitHub Desktop.
A table view cell subclass which uses a nib file to create the content view of the cell. To have more info on how to use it and what is the concept behind it, please visit http://atelierdumobile.com/blog/2011/03/24/loading-custom-uitableviewcells-from-xib
//
// SampleCell.h
//
// Created by Mathieu Godart on 24/03/11.
// Copyright 2011 L'atelier du mobile. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface SampleCell : UITableViewCell {}
@property (nonatomic, retain) IBOutlet UILabel *detailTextLabel;
@property (nonatomic, retain) IBOutlet UILabel *textLabel;
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UIView *content;
// Reuse identifier of this custom cell.
+ (NSString *)reuseIdentifier;
// Cell's default height.
+ (CGFloat)height;
// Init the table cell using a nib file.
- (id)init;
// Configures the subviews of the cell with the given object.
- (void)configureWithObject:(NSManagedObject *)object;
@end
//
// SampleCell.m
//
// Created by Mathieu Godart on 24/03/11.
// Copyright 2011 L'atelier du mobile. All rights reserved.
//
#import "SampleCell.h"
@implementation SampleCell
#pragma mark -
#pragma mark Class methods
// Return the reuse identifier of this custom cell.
+ (NSString *)reuseIdentifier {
return NSStringFromClass([self class]);
}
// Return the name of the nib file to use to init the content view.
+ (NSString *)nibName {
return @"SampleListCell";
// Or you can even use this:
//return [self reuseIdentifier]];
}
// Cell's default height.
+ (CGFloat)height {
return 48 /* Either return a fixed value or get it from the nib file. */;
}
#pragma mark -
#pragma mark Configuring the cell
// Init the table cell using a nib file.
- (id)init {
UITableViewCellStyle style = UITableViewCellStyleDefault;
NSString *identifier = NSStringFromClass([self class]);
if ((self = [super initWithStyle:style reuseIdentifier:identifier])) {
NSString *nibName = [[self class] nibName];
if (nibName) {
[[NSBundle mainBundle] loadNibNamed:nibName
owner:self
options:nil];
NSAssert(self.content != nil, @"NIB file loaded but content property not set.");
[self addSubview:self.content];
}
}
return self;
}
// Configures the subviews of the cell with the given object.
- (void)configureWithObject:(NSManagedObject *)object {
self.textLabel.text = @"My Title";
self.detailTextLabel.text = @"My detail text";
// self.imageView.image = [object myGreatImageToDisplay];
}
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
self.detailTextLabel = nil;
self.textLabel = nil;
self.imageView = nil;
self.content = nil;
[super dealloc];
}
@synthesize detailTextLabel, textLabel, imageView, content;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment